首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过RoutedEvent添加TemplateBinding

通过RoutedEvent添加TemplateBinding
EN

Stack Overflow用户
提问于 2013-08-07 02:36:12
回答 2查看 1.3K关注 0票数 3

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

ModernWindow.cs

代码语言:javascript
复制
/// <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

代码语言:javascript
复制
<!-- 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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-07 13:19:36

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

代码语言:javascript
复制
<Border.InputBindings>
    <MouseBinding Command="presentation:Commands.LogoClickCommand" Gesture="LeftClick" />
</Border.InputBindings>

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

票数 2
EN

Stack Overflow用户

发布于 2013-08-07 05:30:07

我认为,在您的情况下,您可以使用EventSetter,它只是设计来做到这一点。对你来说会是这样的:

代码语言:javascript
复制
<Style TargetType="{x:Type SomeControl}">
    <EventSetter Event="Border.MouseLeftButtonDown" Handler="LogoClick" />
    ...

</Style>

Note: EvenSetter不能通过触发器进行设置,也不能在主题资源字典中包含的样式中使用,因此通常将其放在当前样式的开头。

有关更多信息,请参见:

MSDN中的EventSetter类

或者,如果您需要在ResourceDictionary中使用它,则可以使用不同的方法。创建DependencyProperty (也可以附加)。附随DependencyProperty的示例

财产定义:

代码语言:javascript
复制
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 )的值,那么您将能够完成所需的操作(几乎和事件一样)。

根据事件设置属性的值,您可能希望:

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18093704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档