c# - 与 ListView、UserControl、DependencyProperty、ObservableCollection、INotifyPropertyChanged 的​​绑定(bind)错误

标签 c# wpf xaml binding

我的绑定(bind)不起作用。我搜索了错误,但我不明白如何解决我的问题。

System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'MyApplication.MyUserControl' and 'MyApplication.Person'. Consider using Converter property of Binding. BindingExpression:Path=; DataItem='MyUserControl' (Name=''); target element is 'MyUserControl' (Name=''); target property is 'PersonInfo' (type 'Person')

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='MyApplication.MyUserControl' BindingExpression:Path=; DataItem='MyUserControl' (Name=''); target element is 'MyUserControl' (Name=''); target property is 'PersonInfo' (type 'Person')

基本上它是一个绑定(bind)到类 Person 的 ObservableCollection 的 ListView。

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

最佳答案

我不太明白你为什么要把它搞得那么复杂。您可以在不使用 PersonInfo 属性且不修改其 DataContext 的情况下轻松绑定(bind) UserControl。

<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 中,无需显式绑定(bind)。它的 DataContext 将已经包含一个 Person 对象。

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

关于c# - 与 ListView、UserControl、DependencyProperty、ObservableCollection、INotifyPropertyChanged 的​​绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10163862/

相关文章:

c# - 与任何类型的提供商合作的数据库方法

c# - 如何推断具有继承类型的类型参数?

wpf - 让 WPF 控件垂直拉伸(stretch)

c# - 使用模板 10 在 UWP 用户控件中进行数据绑定(bind)

wpf - 嵌套属性的绑定(bind)静默失败

具有多列的 WPF ItemsControl

c# - 如何在我的所有 Windows 上添加一个公共(public)控件?

c# - 两个应用程序之间的声音交互

c# - WPF。对于多重触发条件, 'Property' 必须具有非空值

c# - 多步算法的设计模式