WPF数据列为结构化数据属性
如何将数据上下文的儿童财产注册为依赖属性?
在下种情况下,我想将两个属性“欧盟”和“模拟”注册为模拟输入的依赖关系属性。这两个属性是缩放类的一部分,这是数据上下文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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要么使用depentencyProperty仿真property或_模拟字段。你不能两者都混合。依赖关系不与_示例字段同步。
如果您的DataContext是AI,则AI类必须从依赖项对象派生。否则模拟input可以容纳依赖项,但是您必须自己同步值(复制缩放值。模拟以模拟property),因此模拟插图充当包装器。
either you use DependencyProperty SimulateProperty or _Simulate field. you can not mix both. The DependencyProperty is not synchronized with the _Simulate field.
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.