wpf C#从另一个依赖项属性更新依赖项属性

时间:2016-03-13 09:36:08

标签: c# wpf dependency-properties

我是WPF的新手,我有以下场景。我想显示一个“压力调节器”的值。然而,在WPF标签中,压力调节器并非必然连接。如果是,我会显示其值,如果不是我想要显示例如," N / A"串。 为了模拟这种情况,我创建了一个类' OutputValues'有两个依赖属性 - PressureRegulatorValue(双精度型)和HasPressureRegualator(bool型):

public class OutputValues : DependencyObject, INotifyPropertyChanged
{
       // ... some stuff
public bool HasPressureRegulator
    {
        get { return (bool)GetValue(HasPressureRegulatorProperty); }
        set { SetValue(HasPressureRegulatorProperty, value); }
    }

    // Using a DependencyProperty as the backing store for HasPressureRegulator.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HasPressureRegulatorProperty =
        DependencyProperty.Register("HasPressureRegulator", typeof(bool), typeof(OutputValues), new PropertyMetadata(false, new PropertyChangedCallback(OutputValues.OnHasPressureRegulatorChanged)));

private static void OnHasPressureRegulatorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        // not sure whether to put any code here
    }

public double PressureRegulatorValue
    {
        get { return (double)GetValue(PressureRegulatorValueProperty); }
        set { SetValue(PressureRegulatorValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for PressureRegulatorValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PressureRegulatorValueProperty =
        DependencyProperty.Register("PressureRegulatorValue", typeof(double), typeof(OutputValues), new PropertyMetadata(0.0));
// ... some more stuff
}

现在我想将我的GUI中的Label(它是UserControl的一部分,名为OutputValuesViewer)绑定到此类的实例。当我将Label绑定到数据源时,数据源应该是我猜的依赖属性,所以在 OutputValuesViewer UserControl中我创建了一个DP:

    public OutputValues OutputValues
    {
        get { return (OutputValues)GetValue(OutputValuesProperty); }
        set { SetValue(OutputValuesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for OutputValues.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty OutputValuesProperty =
        DependencyProperty.Register("OutputValues", typeof(OutputValues), typeof(OutputValuesViewer));

我在 OutputValuesViewer 的构造函数中初始化此属性:

    public OutputValuesViewer()
    {
        InitializeComponent();
        this.OutputValues = new OutputValues();
    }

最后我的XAML看起来像这样:

<UserControl x:Class="OperationDescriptionEditor.OutputValuesViewer"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
xmlns:conversion="clr-namespace:OperationDescriptionEditor">
<!-- Unimportant stuff is left out -->
<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="btvc" />
    <conversion:OutputValuesToPressureRegulatorStringConverter x:Key="ovtprsc" />
</UserControl.Resources>
<!-- More stuff -->
<Label Content="{Binding Path=OutputValues, Mode=OneWay, Converter={StaticResource ovtprsc}, UpdateSourceTrigger=PropertyChanged}" Name="lblPressureRegulatorValue" Grid.Row="0" Grid.Column="1" FontSize="9" />

所以我将标签绑定到OutputValues DP并且我实现了一个转换器,它根据HasPressureRegulator是否为真来创建结果字符串,并且还基于PressureRegulatorValue。我有一种两级DP。 OutputValues是一个DP,因为它返回一个OutputValues类的实例,我可以访问其他两个DP,所以: OutputValues.HasPressureRegulator OutputValues.PressureRegulatorValue

现在我希望如果OutputValues.HasPressureRegulator发生变化,Label会自动刷新自己。但这不会发生。我尝试将PropertyValueChanged回调添加到: OutputValues.HasPressureRegulator - 回调触发 OutputValues - 回调不会触发 所以我猜测Label不知道它应该刷新自己,因为OutputValues属性(Label绑定到的)不会通知它有关更改。 问题是HasPressureRegulator DP应该如何通知其父母&#34; OutputValues DP其值已更改?

从代码中可以看出,我尝试在OutputValues类上实现INotifyPropertyChanged,但这不起作用。

非常感谢!

0 个答案:

没有答案