忽略了代码片段中不相关的内容(比如私有成员声明)
视图模型:
public class LinearGaugeViewModel<T> : INotifyPropertyChanged where T : GaugeElementViewModel
{
public double Value
{
get => _value;
set
{
_value = value;
OnPropertyChanged();
}
}
}
“父”用户控件的XAML:
<ItemsControl ItemsSource="{Binding Elements}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:HorizontalGaugeElementViewModel}">
<local:HorizontalGaugeElement Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Value, Mode=OneWay}"/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
public static DependencyProperty ValueProperty = DependencyProperty.Register(
nameof(Value),
typeof(double),
typeof(HorizontalGaugeElement));
public double Value
{
get => (double) GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
我想要
Value
LinearGaugeViewModel
HorizontalGaugeElement
.
当我更改值时,我可以看到setter
LinearGaugeViewModel.Value
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
仍然是的,dependency属性中的设置从未设置。
我做错什么了?