c# - 将值绑定到 DataTemplate 内的 UserControl 的 DependencyProperty 会返回 UserControl 本身而不是值
问题描述
将值绑定到 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
感谢您的任何帮助。
解决方案
您应该能够稍微简化代码。以下是更改:
FlagView.xaml
<Grid>
<Label Content="{Binding}" Padding="0" Margin="4,0" />
</Grid>
标志单元格模板
<DataTemplate x:Key="FlagCellTemplate">
<ItemsControl ItemsSource="{Binding Flags}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:FlagView/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
FlagView.xaml.cs
public partial class FlagView : UserControl
{
public FlagView()
{
InitializeComponent();
}
}
这将为您提供所需的输出:
推荐阅读
- angular - jsPDF html() 方法拆分 HTML 元素错误
- android - 如何在android Activity中将数据绑定布局附加到root?
- android - 我想在我的笔记本电脑上访问和使用我的安卓 (Ubuntu 20.04)
- sql - 在 sql 中加入,错误 ORA-01722:invalid number
- postgresql - 一个表是否可以引用另一个表并具有引用表中没有的额外键值?
- c - 将2个对象输入C中的链表后出现无限循环
- ios - Firebase Firestore - 带宽费用
- python - API ConnectionRefusedError:[WinError 10061],适用于 Jupyter 而不是 Pycharm
- odoo - odoo 14 - 在 Windows 上:RTL rtlcss 方向 > 解决方案在这里
- css - 如何在 Vuetify 中修复 v-col?