【发布时间】:2018-11-23 19:30:38
【问题描述】:
我在将值绑定到 DataTemplate 中的 UserControl 的 DependencyProperty 时遇到了问题。绑定将提到的 DependencyProperty 设置为对 UserControl 本身的引用,而不是正确的值。由于 DependencyProperty 是字符串类型,因此会显示 UserControl 的完整类名。
<DataTemplate x:Key="FlagCellTemplate">
<ItemsControl ItemsSource="{Binding Flags}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:FlagView FlagString="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
但是,当我使用任何其他控件(例如 Label)时,它可以正常工作。
public partial class FlagView : UserControl, INotifyPropertyChanged {
public static readonly DependencyProperty FlagStringProperty = DependencyProperty.Register(
name: "FlagString",
propertyType: typeof(string),
ownerType: typeof(FlagView),
typeMetadata: new PropertyMetadata(
defaultValue: string.Empty,
propertyChangedCallback: OnFlagStringChanged
)
);
public string FlagString {
set => SetValue(FlagStringProperty, value);
get => (string) GetValue(FlagStringProperty);
}
public FlagView() {
InitializeComponent();
DataContext = this;
}
private static void OnFlagStringChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) =>
((FlagView)source).NotifyPropertyChanged(e.Property.Name);
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
#endregion
}
可在此处找到演示此问题的完整源代码:https://github.com/ProfileRedacted/UserControlBindingBug
感谢您的帮助。
【问题讨论】:
标签: c# wpf xaml data-binding dependency-properties