I want to expose a property that is a string format of two other dependency properties. How do I make this work so anything bound to the derived property is also updated when the true dependency property is updated?
我想公开一个属性,它是另外两个依赖项属性的字符串格式。如何使此工作有效,以便当真正的依赖项属性被更新时,绑定到派生属性的任何内容也被更新?
public static readonly DependencyProperty DeviceProperty =
DependencyProperty.Register("Device", typeof(string), typeof(SlaveViewModel));
public static readonly DependencyProperty ChannelProperty =
DependencyProperty.Register("Channel", typeof(Channels), typeof(SlaveViewModel));
public string Device
{
get { return (string)GetValue(DeviceProperty); }
set { SetValue(DeviceProperty, value); }
}
public Channels Channel
{
get { return (Channels)GetValue(ChannelProperty); }
set { SetValue(ChannelProperty, value); }
}
Now I would like to be able to bind to the following derived property and have it treated as a dependency property:
现在我希望能够绑定到以下派生属性并将其作为依赖属性处理:
public string DeviceDisplay
{
get
{
return string.Format("{0} (Ch #{1})", Device, (int)Channel);
}
}
I'm able to do this by adding callbacks. It works well but it seems a bit verbose. Is there an easier way to do this other than the following?
我可以通过添加回调来实现这一点。它工作得很好,但似乎有点啰嗦。除了下面的方法,还有其他更简单的方法吗?
public static readonly DependencyProperty DeviceProperty =
DependencyProperty.Register("Device", typeof(string), typeof(SlaveViewModel),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.None,
new PropertyChangedCallback(OnDevicePropertyChanged)));
private static void OnDevicePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
SlaveViewModel model = (SlaveViewModel)sender;
model.DeviceDisplay = string.Format(string.Format("{0} (Ch #{1})",
args.NewValue, (int)model.Channel));
}
public static readonly DependencyProperty ChannelProperty =
DependencyProperty.Register("Channel", typeof(Channels), typeof(SlaveViewModel),
new FrameworkPropertyMetadata(Channels.Channel1, FrameworkPropertyMetadataOptions.None,
new PropertyChangedCallback(OnChannelPropertyChanged)));
private static void OnChannelPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
SlaveViewModel model = (SlaveViewModel)sender;
model.DeviceDisplay = string.Format(string.Format("{0} (Ch #{1})",
model.Device, (int)args.NewValue));
}
public static readonly DependencyPropertyKey DeviceDisplayPropertyKey =
DependencyProperty.RegisterReadOnly("DeviceDisplay", typeof(string),
typeof(SlaveViewModel), new FrameworkPropertyMetadata(""));
public static readonly DependencyProperty DeviceDisplayProperty =
DeviceDisplayPropertyKey.DependencyProperty;
public string DeviceDisplay
{
get { return (string)GetValue(DeviceDisplayProperty); }
protected set { SetValue(DeviceDisplayPropertyKey, value); }
}
1 个解决方案
#1
0
I see copy-pasted code, you can just point both callbacks to a single method. Do not know if there is an easier way, but i doubt it.
我看到了复制粘贴的代码,您可以将两个回调都指向一个方法。不知道有没有更简单的办法,但我怀疑。
(Also the typeof(SlaveViewModel)
suggests that this is VM code: I for one would never use DPs in a VM because of thread-affinity.)
(SlaveViewModel)的类型也表明,这是VM代码:对于一个人来说,由于线程亲和性的原因,我永远不会在VM中使用DPs。)
#1
0
I see copy-pasted code, you can just point both callbacks to a single method. Do not know if there is an easier way, but i doubt it.
我看到了复制粘贴的代码,您可以将两个回调都指向一个方法。不知道有没有更简单的办法,但我怀疑。
(Also the typeof(SlaveViewModel)
suggests that this is VM code: I for one would never use DPs in a VM because of thread-affinity.)
(SlaveViewModel)的类型也表明,这是VM代码:对于一个人来说,由于线程亲和性的原因,我永远不会在VM中使用DPs。)