我想在XAML字典中的RoutedEvent上使用Border。RoutedEvent来自模板所在的类,如何实现?
ModernWindow.cs
/// <summary>
/// Gets fired when the logo is clicked.
/// </summary>
public static readonly RoutedEvent LogoClickEvent = EventManager.RegisterRoutedEvent("LogoClickRoutedEventHandler", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ModernWindow));
/// <summary>
/// The routedeventhandler for LogoClick
/// </summary>
public event RoutedEventHandler LogoClick
{
add { AddHandler(LogoClickEvent, value); }
remove { RemoveHandler(LogoClickEvent, value); }
}
/// <summary>
///
/// </summary>
protected virtual void OnLogoClick()
{
RaiseEvent(new RoutedEventArgs(LogoClickEvent, this));
}ModernWindow.xaml
<!-- logo -->
<Border MouseLeftButtonDown="{TemplateBinding LogoClick}" Background="{DynamicResource Accent}" Width="36" Height="36" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,76,0">
<Image Source="{TemplateBinding Logo}" Stretch="UniformToFill" />
</Border>发布于 2013-08-07 13:19:36
我终于找到了一个解决方案,我使用了InputBindings,然后使用了Commands。
<Border.InputBindings>
<MouseBinding Command="presentation:Commands.LogoClickCommand" Gesture="LeftClick" />
</Border.InputBindings>这不是我想要的,但它奏效了:)
发布于 2013-08-07 05:30:07
我认为,在您的情况下,您可以使用EventSetter,它只是设计来做到这一点。对你来说会是这样的:
<Style TargetType="{x:Type SomeControl}">
<EventSetter Event="Border.MouseLeftButtonDown" Handler="LogoClick" />
...
</Style>Note: EvenSetter不能通过触发器进行设置,也不能在主题资源字典中包含的样式中使用,因此通常将其放在当前样式的开头。
有关更多信息,请参见:
MSDN中的EventSetter类
或者,如果您需要在ResourceDictionary中使用它,则可以使用不同的方法。创建DependencyProperty (也可以附加)。附随DependencyProperty的示例
财产定义:
public static readonly DependencyProperty SampleProperty =
DependencyProperty.RegisterAttached("Sample",
typeof(bool),
typeof(SampleClass),
new UIPropertyMetadata(false, OnSample));
private static void OnSample(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool && ((bool)e.NewValue) == true)
{
// do something...
}
}如果您试图设置我们的属性(称为On Sample )的值,那么您将能够完成所需的操作(几乎和事件一样)。
根据事件设置属性的值,您可能希望:
<EventTrigger SourceName="MyBorder" RoutedEvent="Border.MouseLeftButtonDown">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyBorder" Storyboard.TargetProperty="(local:SampleClass.Sample)">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>True</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>https://stackoverflow.com/questions/18093704
复制相似问题