【问题标题】:Add RoutedEvent via TemplateBinding通过 TemplateBinding 添加 RoutedEvent
【发布时间】:2013-08-07 02:36:12
【问题描述】:

我想在 XAML 字典中的 Border 上使用 RoutedEventRoutedEvent 来自模板所在的类,我该如何实现?

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>

【问题讨论】:

    标签: c# wpf border routed-events templatebinding


    【解决方案1】:

    我认为在您的情况下,您可以使用EventSetter,它就是为此而设计的。对你来说,它看起来像这样:

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

    Note: EvenSetter不能通过触发器设置,不能用于主题资源字典中包含的样式,所以通常放在当前样式的开头.

    有关详细信息,请参阅:

    EventSetter Class in MSDN

    或者如果您需要在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>
    

    【讨论】:

    • 谢谢,但这正是问题所在,我需要在自定义用户控件的主题词典中使用它...
    • @Knerd:请参阅有关依赖属性的答案。
    【解决方案2】:

    我终于找到了一个解决方案,我使用了InputBindings,然后是Commands

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

    这不是我想要的,但它有效:)

    【讨论】:

      猜你喜欢
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多