[英]Binding value to a UserControl's DependencyProperty inside a DataTemplate returns UserControl itself instead of the value
I have encountered a problem when binding values to a UserControl's DependencyProperty inside a DataTemplate. 将值绑定到DataTemplate内的UserControl的DependencyProperty时遇到了问题。 The binding is setting mentioned DependencyProperty to a reference to UserControl itself, instead of the right value.
绑定将提到的DependencyProperty设置为对UserControl本身的引用,而不是正确的值。 Since the DependencyProperty is type of string, this results in displaying UserControl's full class name.
由于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>
However when I use any other control, like a Label for example, this works without a problem. 但是,当我使用任何其他控件(例如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
}
A full source code demonstrating this problem can be found here: https://github.com/ProfileRedacted/UserControlBindingBug 可以在这里找到展示此问题的完整源代码: https : //github.com/ProfileRedacted/UserControlBindingBug
Thank you for any help. 感谢您的任何帮助。
You should be able to simplify the code a bit. 您应该能够简化代码。 Here's the changes:
这是更改:
FlagView.xaml FlagView.xaml
<Grid>
<Label Content="{Binding}" Padding="0" Margin="4,0" />
</Grid>
FlagCellTemplate FlagCellTemplate
<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 FlagView.xaml.cs
public partial class FlagView : UserControl
{
public FlagView()
{
InitializeComponent();
}
}
This will give you the desired output: 这将为您提供所需的输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.