WPF 数据绑定到结构化数据 属性

WPF databinding to structured data property

如何将数据上下文的子项 属性 注册为依赖项 属性?

在以下情况下,我想将“EU”和“Simulate”这两个属性注册为模拟输入的依赖属性。这两个属性是 Scaling class 的一部分,Scaling class 是 Data Context AI Class 的一部分。我该怎么做?

XAML侧:

<TextBlock Text="{Binding EU, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AnalogInput}}}"
                                                       FontSize="11" FontFamily="Verdana" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center" />

后面的代码:

public class AnalogInput : BaseUserDynamo
{
    public static readonly DependencyProperty EUProperty = DependencyProperty.Register("EU", typeof(float), typeof(Scaling));
    public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(AnalogInput));
    public static readonly DependencyProperty SimulateProperty = DependencyProperty.Register("Simulate", typeof(bool), typeof(Scaling));
    public static readonly DependencyProperty ResolutionProperty = DependencyProperty.Register("Resolution", typeof(int), typeof(AnalogInput));

    static AnalogInput()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(AnalogInput), new FrameworkPropertyMetadata(typeof(AnalogInput)));
    }

    public AnalogInput()
    {
        try
        {
            AI myAI = new AI();
            DataContext = myAI;
            myAI.PropertyChanged += MyObj_PropertyChanged;
      
        }
        catch (Exception)
        {
            throw;
        }

    }
}

缩放是 AI 的一部分 class。

internal class AI : PLCBaseObject
{
    private Scaling _ScaleIn;
    private string _Unit;
    private int _Resolution;

    public Scaling ScaleIn { get { return _ScaleIn; } set { if (value != _ScaleIn) { _ScaleIn = value; OnPropertyChanged(); } } }
    public string Unit { get { return _Unit; } set { if (value != _Unit) { _Unit = value; OnPropertyChanged(); } } }
    public int Resolution { get { return _Resolution; } set { if (value != _Resolution) { _Resolution = value; OnPropertyChanged(); } } }
}

缩放 Class 有很多属性。

public class Scaling : PLCBaseObject
{
    private bool _Simulate;
    private float _EU;

    public bool Simulate { get { return _Simulate; } set { if (value != _Simulate) { _Simulate = value; OnPropertyChanged(); } } }
    public float EU { get { return _EU; } set { if (value != _EU) { _EU = value; OnPropertyChanged(); } } }

}

您可以使用 DependencyProperty SimulateProperty 或 _Simulate 字段。你不能混合两者。 DependencyProperty 与 _Simulate 字段不同步。

// class Scaling 
public static readonly DependencyProperty SimulateProperty = DependencyProperty.Register(
        "Simulate", typeof(bool), typeof(Scaling), new PropertyMetadata(default(bool)));

public bool Simulate {
    get { return (bool) GetValue(SimulateProperty); }
    set { SetValue(SimulateProperty, value); }
}

如果您的数据上下文应为 AI,则 AI class 必须派生自 DependencyObject。否则 AnalogInput 可以保存 DependencyProperty,但是您必须自己同步这些值(将 Scaling.Simulate 的值复制到 SimulateProperty)以便 AnalogInput 充当包装器。

// class AnalogInput
public static readonly DependencyProperty SimulateProperty = DependencyProperty.Register(
        "Simulate", typeof(bool), typeof(AnalogInput), new PropertyMetadata(default(bool)));

public bool Simulate {
    get { return (bool) GetValue(SimulateProperty); }
    set { SetValue(SimulateProperty, value); }
}