DependencyProperty不更新

时间:2017-08-15 09:54:54

标签: c# wpf xaml dependency-properties

我有一个带有IsChecked属性的用户控件

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
        "IsChecked", typeof(bool), typeof(LabeledCheckbox),new FrameworkPropertyMetadata
    {
        DefaultValue = false,
        BindsTwoWayByDefault = true,

    });

    public bool IsChecked
    {
        get { return (bool)this.GetValue(IsCheckedProperty); }
        set { this.SetValue(IsCheckedProperty, value); }
    }

我将IsChecked属性绑定到另一个名为active的属性 我创建了一个复选框,IsChecked属性也绑定到活动属性

<controls:LabeledCheckbox x:Name="ccc" Tag="Active" CheckedColor=" #33cc00" UncheckedColor="#cc2900" CheckedContent="hello" UncheckedContent="world" IsChecked="{Binding Active, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" FontSize="25" Margin="5" Width="100"/>
<CheckBox IsChecked="{Binding Active, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

复选框已正确更新,但控件:LabeledCheckbox未更新 控件中的其他DP:LabeledCheckbox工作得很好。

为什么IsChecked属性没有更新? 试图使用RelativeSource = {RelativeSource AncestorType = {x:Type Window}} 在控件上:LabeledCheckbox像这样:

<controls:LabeledCheckbox x:Name="ccc" Tag="Active" CheckedColor=" #33cc00" UncheckedColor="#cc2900" CheckedContent="שלום" UncheckedContent="עולם" IsChecked="{Binding Active, RelativeSource={RelativeSource AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" FontSize="25" Margin="5" Width="100"/>

尝试了一种方式来源

除非我直接设置True ao false并且没有绑定,否则永远不会触发IsChecked的集合。

更新 添加UserControl的完整代码:

public partial class LabeledCheckbox : UserControl
{
    public LabeledCheckbox()
    {
        InitializeComponent();            
    }

    public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
        "IsChecked", typeof(bool), typeof(LabeledCheckbox),new FrameworkPropertyMetadata
    {
        DefaultValue = false,
        BindsTwoWayByDefault = true,
        PropertyChangedCallback = CheckChanged

    });

    private static void CheckChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show("Change");
    }

    public bool IsChecked
    {
        get { return (bool)this.GetValue(IsCheckedProperty); }
        set { this.SetValue(IsCheckedProperty, value); }
    }

    public static readonly DependencyProperty CheckedColorProperty = DependencyProperty.Register(
        "CheckedColor", typeof(Color), typeof(LabeledCheckbox), new PropertyMetadata(Colors.White));

    public Color CheckedColor
    {
        get { return (Color)this.GetValue(CheckedColorProperty); }
        set { this.SetValue(CheckedColorProperty, value); }
    }

    public static readonly DependencyProperty CheckedContentProperty = DependencyProperty.Register(
        "CheckedContent", typeof(string), typeof(LabeledCheckbox), null);

    public string CheckedContent
    {
        get { return (string)this.GetValue(CheckedContentProperty); }
        set { this.SetValue(CheckedContentProperty, value); }
    }

    public static readonly DependencyProperty UncheckedColorProperty = DependencyProperty.Register(
        "UncheckedColor", typeof(Color), typeof(LabeledCheckbox), new PropertyMetadata(Colors.White));

    public Color UncheckedColor
    {
        get { return (Color)this.GetValue(UncheckedColorProperty); }
        set { this.SetValue(UncheckedColorProperty, value); }
    }
    public static readonly DependencyProperty UncheckedContentProperty = DependencyProperty.Register(
        "UncheckedContent", typeof(string), typeof(LabeledCheckbox), null);

    public string UncheckedContent
    {
        get { return (string)this.GetValue(UncheckedContentProperty); }
        set { this.SetValue(UncheckedContentProperty, value); }
    }


    public static readonly RoutedEvent CheckedChangeEvent = EventManager.RegisterRoutedEvent("CheckedChange", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(LabeledCheckbox));

    public event RoutedEventHandler CheckedChange
    {
        add { AddHandler(CheckedChangeEvent, value); }
        remove { RemoveHandler(CheckedChangeEvent, value); }
    }

    private void ToggleCheckbox(object sender, MouseButtonEventArgs e)
    {
        IsChecked = !IsChecked;
        AnimateCheckbox();
        RaiseCheckedChangeEvent();
    }

    private void AnimateCheckbox()
    {
        DoubleAnimation cAnim = null;
        DoubleAnimation ucAnim = null;
        if (IsChecked)
        {
            cAnim = AnimationsHandler.AnimateElementWidth(0, this.Width, TimeSpan.FromMilliseconds(400));
            ucAnim = AnimationsHandler.AnimateElementWidth(this.Width, 0, TimeSpan.FromMilliseconds(400));
        }
        else
        {
            cAnim = AnimationsHandler.AnimateElementWidth(this.Width, 0, TimeSpan.FromMilliseconds(400));
            ucAnim = AnimationsHandler.AnimateElementWidth(0, this.Width, TimeSpan.FromMilliseconds(400));
        }
        var sb = new Storyboard();
        AnimationsHandler.AddToStoryboard(sb, cAnim, check, new PropertyPath(FrameworkElement.WidthProperty));
        AnimationsHandler.AddToStoryboard(sb, ucAnim, uncheck, new PropertyPath(FrameworkElement.WidthProperty));
        sb.Begin();
    }

    void RaiseCheckedChangeEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(LabeledCheckbox.CheckedChangeEvent);
        RaiseEvent(newEventArgs);
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        ToggleCheckbox(null, null);
    }
}

0 个答案:

没有答案