他の2つの依存関係プロパティの文字列形式であるプロパティを公開したいと思います。真の依存関係プロパティが更新されたときに、派生プロパティにバインドされているものもすべて更新されるように、これを機能させるにはどうすればよいですか?
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); }
}
ここで、次の派生プロパティにバインドして、依存関係プロパティとして処理できるようにしたいと思います。
public string DeviceDisplay
{
get
{
return string.Format("{0} (Ch #{1})", Device, (int)Channel);
}
}
コールバックを追加することでこれを行うことができます。それはうまく機能しますが、少し冗長に見えます。以下以外にこれを行う簡単な方法はありますか?
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); }
}