【问题标题】:Binding value to a UserControl's DependencyProperty inside a DataTemplate returns UserControl itself instead of the value将值绑定到 DataTemplate 内的 UserControl 的 DependencyProperty 会返回 UserControl 本身而不是值
【发布时间】: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


    【解决方案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();
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2012-06-06
      • 1970-01-01
      • 2013-06-03
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2016-05-13
      相关资源
      最近更新 更多