触发的datatrigger不会更改自定义控件的新添加属性

时间:2013-09-24 20:59:26

标签: c# wpf xaml

我正在创建一个动画控件,我正在尝试使用数据触发器。问题是触发器触发时我创建的dp属性没有被更改/调用。以下是我注意到的行为摘要。

1)背后的代码永远不会被调用。

2)属性出现在XAML intellisense中,但XAML中给出的更改永远不会被应用(设计/运行时)。但是如果我在“public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register(”IsSpinning“,typeof(bool),typeof(ProgressWaitSpinner),new UIPropertyMetadata(false))中替换'IsSpinning';”到其他东西(比如'xyz')它开始为属性赋值工作,但是如果启用了样式则抛出运行时异常。

3)运行样本时,应该隐藏矩形而不是显示为巧克力色,这不会发生。

4)用于更改颜色的Setter正在工作,它来自用户控件,但是新创建的属性上的setter属性不起作用。

我在这里创建了一个简化的示例,显示了问题。有人知道发生了什么事吗?

UserControl XAML:

<UserControl x:Class="CustomControls.ProgressWaitSpinner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControls" 
    Height="191" Width="191">
    <Grid x:Name="LayoutRoot">
        <Label Height="32" Name="label1" VerticalAlignment="Top" />
    </Grid>
</UserControl>

UserControl代码:

using System.Windows;
using System.Windows.Controls;

namespace CustomControls
{
    public partial class ProgressWaitSpinner : UserControl
    {

        public ProgressWaitSpinner(){InitializeComponent();}

        public bool IsSpinning
        {
            get 
            {
                return (bool)GetValue(IsSpinningProperty); 
            }
            set
            {
                if (value == true)
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Hidden;
                }
                SetValue(IsSpinningProperty, value); 
            }
        }

        public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register("IsSpinning", typeof(bool), typeof(ProgressWaitSpinner), new UIPropertyMetadata(false));
    }
}

MainWindow XAML:

<Window x:Class="WPFSpinnerWait.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:usrctrl="clr-namespace:CustomControls"
        Title="MainWindow" Height="208" Width="228">
    <Grid>
        <usrctrl:ProgressWaitSpinner Height="40" x:Name="WaitSpinner" Margin="110,103,0,0" HorizontalAlignment="Left" Width="84" VerticalAlignment="Top">
            <usrctrl:ProgressWaitSpinner.Style>
                <Style>
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=label1, Path=Content}" Value="NotStarted"></Condition>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="usrctrl:ProgressWaitSpinner.Background" Value="Red" />
                            <Setter Property="usrctrl:ProgressWaitSpinner.IsSpinning" Value="false"/>
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=label1, Path=Content}" Value="Running"></Condition>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="usrctrl:ProgressWaitSpinner.Background" Value="Chocolate" />
                            <Setter Property="usrctrl:ProgressWaitSpinner.IsSpinning" Value="true" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </usrctrl:ProgressWaitSpinner.Style>
        </usrctrl:ProgressWaitSpinner>
        <Button Content="NotStarted" Height="28" HorizontalAlignment="Left" Margin="38,22,0,0" Name="checkBox1" VerticalAlignment="Top" Width="136" Click="checkBox1_Checked" />
        <Button Content="Running" Height="30" HorizontalAlignment="Left" Margin="38,56,0,0" Name="checkBox2" VerticalAlignment="Top" Width="136" Click="checkBox1_Checked" />
        <Label Content="NotStarted" DataContext="usrctrl:ProgressWaitSpinner" Height="25" HorizontalAlignment="Left" Margin="38,92,0,0" Name="label1" VerticalAlignment="Top" Width="114" />
    </Grid>
</Window>

MainWindow代码:

using System.Windows;
using System.Windows.Controls;

namespace WPFSpinnerWait
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            label1.Content = ((Button)sender).Content.ToString();
        }

    }
}

1 个答案:

答案 0 :(得分:2)

赢得后面的代码被调用,DependancyProperties在Xaml中更改/使用属性时不使用backing属性,但它只用于代码在作为帮助者的背后,thay在Xaml绑定中没有用处

您可以使用PropertyChanged的{​​{1}}事件代替

DependancyProperty

编辑:

关于第二个问题,请尝试为 public bool IsSpinning { get { return (bool)GetValue(IsSpinningProperty); } set { SetValue(IsSpinningProperty, value); } } // Using a DependencyProperty as the backing store for IsSpinning. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsSpinningProperty = DependencyProperty.Register("IsSpinning", typeof(bool), typeof(ProgressWaitSpinner), new PropertyMetadata(false, OnIsSpinningChanged)); private static void OnIsSpinningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (((bool)e.NewValue) == true) { (d as ProgressWaitSpinner).Visibility = System.Windows.Visibility.Visible; } else { (d as ProgressWaitSpinner).Visibility = System.Windows.Visibility.Hidden; } } 添加TargetType,以便直接访问这些属性

Style