Binding Error with ListView, UserControl, DependencyProperty, ObservableCollection, INotifyPropertyChanged

My binding does not work. I was looking for an error, but I don’t understand how to fix it in my case.

System.Windows.Data error: 1: it is not possible to create a default converter for performing one-way conversions between the types "MyApplication.MyUserControl" and "MyApplication.Person". Consider using the Binding transform property. BindingExpression: Path =; DataItem = 'MyUserControl' (Name = ''); target element - "MyUserControl" (Name = ''); target is "PersonInfo" (type "Person")

System.Windows.Data error: 5: The value generated by BindingExpression is not valid for the target property; Value = 'MyApplication.MyUserControl' BindingExpression: Path =; DataItem = 'MyUserControl' (Name = ''); target element - "MyUserControl" (Name = ''); target is "PersonInfo" (type "Person")

This is basically a ListView associated with the ObservableCollection of the Person class.

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public ObservableCollection<Person> PersonCollection { set; get; }

    public MainWindow()
    {
        PersonCollection = new ObservableCollection<Person>();
        InitializeComponent();
        PersonCollection.Add(new Person() { Name = "Bob", Age = 20 });
    }
}

MainWindow.xaml

<Window DataContext="{Binding RelativeSource={RelativeSource Self}}" xmlns:self="clr-namespace:MyApplication" x:Class="MyApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView ItemsSource="{Binding PersonCollection}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <self:MyUserControl PersonInfo="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
</Window>

MyUserControl.xaml.cs

public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty PersonProperty = DependencyProperty.Register("PersonInfo", typeof(Person), typeof(MyUserControl));

    public Person PersonInfo
    {
        get { return (Person)GetValue(PersonProperty); }
        set { SetValue(PersonProperty, value); }
    }

    public MyUserControl()
    {
        InitializeComponent();
    }
}

Myusercontrol.xaml

<UserControl DataContext="{Binding RelativeSource={RelativeSource Self}}" x:Class="MyApplication.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<TextBlock Text="{Binding PersonInfo.Name}" />
</UserControl>

Person.cs

public class Person : INotifyPropertyChanged
{
    public int Age { set; get; }
    public string Name { set; get; }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}
+3
source share
3 answers

I can’t understand why you would complicate this. You can easily bind your UserControl without a property PersonInfoand without changing its DataContext.

<UserControl x:Class="MyApplication.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> 
    <TextBlock Text="{Binding Name}" /> 
</UserControl>

UserControl DataTemplate . DataContext Person.

<DataTemplate>      
    <StackPanel>      
        <self:MyUserControl />      
    </StackPanel>      
</DataTemplate>      
+3

, , :

- ObservableObject.cs

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
            handler(this, e);
    }

    protected void SetValue<T>(ref T field, T value, string propertyName)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}

MainWindow - MainWindowModel.cs

public class MainWindowModel : ObservableObject
{
    private readonly ObservableCollection<Person> personCollection = new ObservableCollection<Person>()
    {
        new Person() { Name = "Bob", Age = 20 }
    };

    public ObservableCollection<Person> PersonCollection
    {
        get { return this.personCollection; }
    }
}

MainWindow.xaml.cs .

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

MainWindow.xaml DataContext MainWindowModel.

<Window x:Class="MyApplication.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:self="clr-namespace:MyApplication">
    <Window.DataContext>
        <self:MainWindowModel/>
    </Window.DataContext>
    <ListView ItemsSource="{Binding PersonCollection}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <self:MyUserControl/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Window>

MyUserControl.xaml.cs ( ).

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

MyUserControl.xaml

<UserControl x:Class="MyApplication.MyUserControl" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TextBlock Text="{Binding Name}"/>
</UserControl>

Person.cs

public class Person : ObservableObject
{
    private int age;
    private string name;

    public int Age
    {
        get { return this.age; }
        set { this.SetValue(ref this.age, value, "Age"); }
    }

    public string Name
    {
        get { return this.name; }
        set { this.SetValue(ref this.name, value, "Name"); }
    }
}
+2

UserControl XAML

<UserControl x:Class="MyApplication.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <TextBlock DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"
               Text="{Binding PersonInfo.Name}" />
</UserControl>

DataContext.

+1

All Articles