C# NotifyPropertyChanged isn';t更新CustomControl中的DependencyProperty
我用DPC# NotifyPropertyChanged isn';t更新CustomControl中的DependencyProperty,c#,wpf,custom-controls,dependency-properties,inotifypropertychanged,C#,Wpf,Custom Controls,Dependency Properties,Inotifypropertychanged,我用DP阈值制作了一个CustomControl,如下所示: public class SymbolControl : Control { public static readonly DependencyProperty ThresholdProperty = DependencyProperty.Register("Threshold", typeof(IThreshold<SolidColorBrush>), typeof(SymbolControl)); p
阈值制作了一个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); }
}
...
}
下面是CustomControl的使用方法:
<controls:SymbolControl ... Threshold="{Binding Threshold, NotifyOnTargetUpdated=True, Converter={StaticResource DummyConverter}}" .../>
我在网上做了很多搜索,但运气不好,你知道问题可能是什么吗?我找到了另一个解决方法,因为我无法使用问题中给出的其他解决方法:
我将代码添加到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; }
}
但是,此解决方案并不理想,因此我不会接受它,如果有人找到更好的解决方案,请让我知道:)阈值依赖项属性忽略任何更新,只要您不将其值替换为新的IThreshold
实例,即{Binding Threshold}
提供不同的对象(或null)。这是故意的。@Clemens哦,我不知道,有什么办法可以解决这个问题吗?是的,按照您之前的做法,即设置一个临时空值。问题是,如果IThreshold
实例中的某个属性发生更改,我找不到执行该位代码的方法,我已尝试将其添加到PropertyChanged
事件中,如\u threshold.PropertyChanged+=threshold\u PropertyChanged设置程序中的代码>(其中无效阈值\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
将其临时设置为null),但这不起作用,因为它会将自身设置为null,因此最后一行(阈值=临时;
)将不会执行您可以更改多重绑定(及其转换器)使其绑定到阈值的各个属性,例如
。当然,这些属性还必须触发INotifyPropertyChanged接口的PropertyChanged事件。
var temp = Threshold;
Threshold = null;
Threshold = temp;
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; }
}
...
<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>
...