将值绑定到DataTemplate内的UserControl的DependencyProperty会返回UserControl本身而不是值

时间:2018-06-14 05:46:27

标签: c# wpf xaml data-binding dependency-properties

我在将值绑定到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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您应该能够简化代码。这是变化:

<强> FlagView.xaml

<Grid>
    <Label Content="{Binding}" Padding="0" Margin="4,0" />
</Grid>

<强> 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

public partial class FlagView : UserControl
{
    public FlagView()
    {
        InitializeComponent();
    }
}

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

DataContext UserControl inside ItemsControl

相关问题