繁体   English   中英

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

[英]Binding value to a UserControl's DependencyProperty inside a DataTemplate returns UserControl itself instead of the value

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

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

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

ItemsControl内的DataContext UserControl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM