简体   繁体   中英

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. The binding is setting mentioned DependencyProperty to a reference to UserControl itself, instead of the right value. Since the DependencyProperty is type of string, this results in displaying UserControl's full class name.

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

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

Thank you for any help.

You should be able to simplify the code a bit. Here's the changes:

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

This will give you the desired output:

ItemsControl内的DataContext UserControl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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