DataTrigger不会在Button的派生类上触发 [英] DataTrigger does not fire on a derived class of a Button

查看:123
本文介绍了DataTrigger不会在Button的派生类上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在应用程序中有大量带有图标和文本的按钮,我正在尝试为所有按钮提取一种通用样式.我想到了从Button派生的想法,并具有几个依赖项属性.此自定义按钮类具有我们应用程序中所有按钮通用的样式.

We have a ton of Buttons with an Icon and Text across the App, and I am trying to extract a common style for all of them. I came up with this idea of deriving from a Button and have a couple of dependency properties. This custom button class has a style common to all the Buttons in our Application.

我的自定义按钮定义:

<Button x:Class="UIElements.Controls.CustomToolBarButton"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
               xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" mc:Ignorable="d"
               >
<!--Style="{StaticResource ResourceKey=ToolBarButtonStyle}"-->
<StackPanel Orientation="Horizontal">
    <Image Source="{Binding Icon, Mode=TwoWay}" Style="{StaticResource ResourceKey=ToolBarButtonIconStyle}" />
    <TextBlock Text="{Binding DisplayText, Mode=TwoWay}" Style="{StaticResource ResourceKey=ToolBarButtonDisplayTextStyle}" />
</StackPanel>

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
//using Telerik.Windows.Controls;

namespace UIElements.Controls
{
    public partial class CustomToolBarButton : Button
    {
        public CustomToolBarButton()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(BitmapImage), typeof(Button), new PropertyMetadata(default(BitmapImage)));

        public BitmapImage Icon
        {
            get { return (BitmapImage)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty DisplayTextProperty =
            DependencyProperty.Register("DisplayText", typeof(string), typeof(Button), new PropertyMetadata(default(string)));

        public string DisplayText
        {
            get { return (string) GetValue(DisplayTextProperty); }
            set { SetValue(DisplayTextProperty, value); }
        }
    }
}

我正在按如下方式使用此控件:

<Controls1:CustomToolBarButton Icon="{DynamicResource ImageSave}"
                        DisplayText="Test Display">
<Controls1:CustomToolBarButton.Style>
    <Style TargetType="Controls1:CustomToolBarButton">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding SelectedListItem.ListId}" Value="0" />
                </MultiDataTrigger.Conditions>
                <!--<Setter Property="Button.IsEnabled" Value="False" />-->
                <Setter Property="Background" Value="BurlyWood" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</Controls1:CustomToolBarButton.Style>

情况看起来不错,但是当有界数据更改时,触发器不会触发.如果我将相同的触发器应用于常规按钮,则似乎工作正常.

Things look good, however the trigger is not firing when the bounded data changes. If I apply the same trigger to the regular button, it seems to be working fine.

您能告诉我我是否缺少什么吗?

Could you please tell me if I am missing something?

找到了优雅的解决方案.利用附加属性

Found an elegant solution. Made use of Attached Properties

推荐答案

首先,您应该

First of all you should never use this.DataContext = this;, ever.

第二,如果您从按钮继承,则不应创建XAML文件,而应在Themes/Generic.xaml资源字典中创建默认样式(如果您从UserControl继承,则可以使用这样的文件).

Secondly if you inherit from button you should not create a XAML file but a default style in the Themes/Generic.xaml resource dictionary (if you inherit from UserControl a file like this is fine).

这也意味着您将需要定义 RelativeSource ).

This will also mean that you will need to define a Control.Template in the style and hence should use TemplateBindings instead of normal ones (or use RelativeSource).

另一个答案所述,您可能需要覆盖元数据.

As noted in another answer you may need to override the metadata.

这篇关于DataTrigger不会在Button的派生类上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆