首页 > 解决方案 > 将值绑定到 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

感谢您的任何帮助。

标签: c#wpfxamldata-bindingdependency-properties

解决方案


您应该能够稍微简化代码。以下是更改:

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();
    }
}

这将为您提供所需的输出:

ItemsControl 内的 DataContext UserControl


推荐阅读