NotifyPropertyChanged未在CustomControl中更新DependencyProperty

问题描述 投票:0回答:1

我使用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中使用Property的位置:

...
        <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;

我在网上做了很多搜索,没有运气,对这个问题有什么想法?

c# wpf custom-controls dependency-properties inotifypropertychanged
1个回答
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>
...

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

© www.soinside.com 2019 - 2024. All rights reserved.