WPF数据列为结构化数据属性

发布于 2025-01-23 23:56:20 字数 2424 浏览 3 评论 0原文

如何将数据上下文的儿童财产注册为依赖属性?

在下种情况下,我想将两个属性“欧盟”和“模拟”注册为模拟输入的依赖关系属性。这两个属性是缩放类的一部分,这是数据上下文AI类的一部分。我该怎么做?

在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类的一部分。

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(); } } }
}

缩放类具有许多属性。

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(); } } }

}

How can I register a child property of the data context as dependency property?

In the following case, I want to register the two properties "EU" and "Simulate" as dependency properties of Analog Input. Those two properties are part of Scaling class which is a part of the Data Context AI Class. How can I do that?

At the XAML side:

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

The code behind:

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

    }
}

The scaling is a part of the 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(); } } }
}

The Scaling Class has a bunch of properties.

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(); } } }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧街凉风 2025-01-30 23:56:20

您要么使用depentencyProperty仿真property或_模拟字段。你不能两者都混合。依赖关系不与_示例字段同步。

// 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); }
}

如果您的DataContext是AI,则AI类必须从依赖项对象派生。否则模拟input可以容纳依赖项,但是您必须自己同步值(复制缩放值。模拟以模拟property),因此模拟插图充当包装器。

// 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); }
}

either you use DependencyProperty SimulateProperty or _Simulate field. you can not mix both. The DependencyProperty is not synchronized with the _Simulate field.

// 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); }
}

if your datacontext shall be AI, the AI class must derive from DependencyObject. else AnalogInput can hold the DependencyProperty but then you must synchronize the values yourself (copy the value of Scaling.Simulate to SimulateProperty) so AnalogInput acts as wrapper.

// 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); }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文