0

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

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

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

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

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

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

用户控件 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>

用户控制代码:

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));
    }
}

主窗口 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>

主窗口代码:

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();
        }

    }
}
4

1 回答 1

2

后面的代码不会被调用,DependancyProperties当属性在 Xaml 中更改/使用时不要使用支持属性,它们仅用于在后面的代码中作为帮助程序,在 Xaml 绑定中没有用

您可以使用的PropertyChanged事件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以便您可以直接访问属性

 <Style TargetType="{x:Type usrctrl:ProgressWaitSpinner}">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding ElementName=label1, Path=Content}" Value="NotStarted"></Condition>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Red" />
            <Setter Property="IsSpinning" Value="false"/>
        </MultiDataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding ElementName=label1, Path=Content}" Value="Running"></Condition>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Chocolate" />
            <Setter Property="IsSpinning" Value="true" />
        </MultiDataTrigger>
    </Style.Triggers>
</Style>
于 2013-09-24T21:09:14.603 回答