C# 通过TemplateBinding添加RouteEvent

C# 通过TemplateBinding添加RouteEvent,c#,wpf,border,routed-events,templatebinding,C#,Wpf,Border,Routed Events,Templatebinding,我想在XAML字典的边框上使用RoutedEvent。RoutedEvent来自模板所在的类,如何实现这一点 现代印度 /// <summary> /// Gets fired when the logo is clicked. /// </summary> public static readonly RoutedEvent LogoClickEvent = EventManager.RegisterRoutedEvent("LogoClickRoutedEventHa

我想在XAML字典的
边框上使用
RoutedEvent
RoutedEvent
来自模板所在的类,如何实现这一点

现代印度

/// <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));
}
//
///单击徽标时被激发。
/// 
公共静态只读RoutedEvent LogoClickEvent=EventManager.RegisterRoutedEvent(“LogoClickRoutedEventHandler”,RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(ModernWindow));
/// 
///用于LogoClick的routedeventhandler
/// 
公共事件路由EventHandler徽标单击
{
添加{AddHandler(LogoClickEvent,value);}
删除{RemoveHandler(LogoClickEvent,value);}
}
/// 
/// 
/// 
受保护的虚拟void OnLogoClick()
{
RaiseEvent(新路由EventArgs(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>

我认为在您的情况下,您可以使用
事件设置器,它正是为了实现这一点而设计的。对你来说,它看起来像这样:

<Style TargetType="{x:Type SomeControl}">
    <EventSetter Event="Border.MouseLeftButtonDown" Handler="LogoClick" />
    ...

</Style>
如果您尝试设置我们的属性值,即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>

真的

我终于找到了一个解决方案,我使用了
输入绑定
命令

<Border.InputBindings>
    <MouseBinding Command="presentation:Commands.LogoClickCommand" Gesture="LeftClick" />
</Border.InputBindings>


这不是我想要的,但它可以工作:)

Thx,但这正是问题所在,我需要在自定义用户控件的主题词典中使用它…@Knerd:请参见关于dependency属性的答案。
<Border.InputBindings>
    <MouseBinding Command="presentation:Commands.LogoClickCommand" Gesture="LeftClick" />
</Border.InputBindings>