NotifyPropertyChanged不会在CustomControl中更新DependencyProperty

时间:2019-04-17 11:03:54

标签: c# wpf custom-controls dependency-properties inotifypropertychanged

我已经用DP Threshold制作了一个CustomControl,如下所示:

public class SymbolControl : Control
{
    public static readonly DependencyProperty ThresholdProperty = DependencyProperty.Register("Threshold", typeof(IThreshold<SolidColorBrush>), typeof(SymbolControl));

    public IThreshold<SolidColorBrush> Threshold
    {
        get { return (IThreshold<SolidColorBrush>)GetValue(ThresholdProperty); }
        set { SetValue(ThresholdProperty, value);  }
    }
...
}

在此处是自定义控件的xaml中使用属性的地方:

...
        <Border.Background>
            <MultiBinding Converter="{StaticResource ThreshholdToReturnValueConverter}" NotifyOnTargetUpdated="True" >
                <Binding Path="Threshold" RelativeSource="{RelativeSource TemplatedParent}" NotifyOnTargetUpdated="True" />
                <Binding Path="SymbolValue" RelativeSource="{RelativeSource TemplatedParent}" NotifyOnTargetUpdated="True" />
                <Binding Path="DefaultBackground" RelativeSource="{RelativeSource TemplatedParent}" NotifyOnTargetUpdated="True" />
            </MultiBinding>
        </Border.Background>
...

这是CustomControl的使用方式:

<controls:SymbolControl ... Threshold="{Binding Threshold, NotifyOnTargetUpdated=True, Converter={StaticResource DummyConverter}}" .../>

当我致电NotifyPropertyChanged(nameof(Threshold))时,CustomControl不会更新。

但是,当实例化自定义控件时,我在Threshold绑定中放置了一个带有断点的虚拟转换器,并且当我调用NotifyPropertyChanged(nameof(Threshold))时触发了该断点,因此看来绑定没有更新目标?

我还尝试为DP PropertyChangedCallback添加一个带有断点的ThresholdProperty,该断点仅在首次实例化原始属性时触发。

我还发现在ViewModel中这样做会导致自定义控件更新:

var temp = Threshold;
Threshold = null;
Threshold = temp;

我已经做了很多在线搜索,但是没有运气,对这个问题可能有什么想法?

1 个答案:

答案 0 :(得分:0)

我找到了另一种解决方法,因为我无法使用问题中给出的其他解决方法:

我将代码添加到了Threshold.cs

public Threshold : IThreshold<SolidColorBrush>, INotifyPropertyChanged
{
...
    public Threshold()
    {
        ...
        this.PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) { if (e.PropertyName != nameof(WorkAround)) { NotifyPropertyChanged(nameof(WorkAround)); } };
    }
...
public bool WorkAround { set; get; }
}

在自定义控件的xaml中,我向MultiBinding添加了第四个绑定:

...
        <Border.Background>
            <MultiBinding Converter="{StaticResource ThreshholdToReturnValueConverter}" NotifyOnTargetUpdated="True" >
                <Binding Path="Threshold" RelativeSource="{RelativeSource TemplatedParent}" NotifyOnTargetUpdated="True" />
                <Binding Path="SymbolValue" RelativeSource="{RelativeSource TemplatedParent}" NotifyOnTargetUpdated="True" />
                <Binding Path="DefaultBackground" RelativeSource="{RelativeSource TemplatedParent}" NotifyOnTargetUpdated="True" />
                <Binding Path="Threshold.WorkAround" RelativeSource="{RelativeSource TemplatedParent}" NotifyOnTargetUpdated="True" />
            </MultiBinding>
        </Border.Background>
...

但是这种解决方法并不理想,所以我不会接受,如果有人找到更好的解决方案,请告诉我:)