C# 在主窗口中绑定对象
请记住标题,如果您有更好的,请编辑 如果在资源中使用wpf中创建的对象,我有一个工作代码,如下所示:C# 在主窗口中绑定对象,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,请记住标题,如果您有更好的,请编辑 如果在资源中使用wpf中创建的对象,我有一个工作代码,如下所示: <local:PersonView x:Key="Persons"/> 我尝试了这个,但不起作用: <CollectionViewSource x:Key="ViewPersons" Source="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=BindThis}">
<local:PersonView x:Key="Persons"/>
我尝试了这个,但不起作用:
<CollectionViewSource x:Key="ViewPersons" Source="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=BindThis}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Name"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
Sry伙计们,我花了很长时间试图解决这个问题。但刚才我让它开始工作了。这件事仍然让我很困惑。希望它能帮助别人 修复方法:
public class PersonView : ObservableCollection<Person>
{
public PersonView()
{
Add(new Person() { Name = "Luis" });
Add(new Person() { Name = "Gusth" });
}
}
公共类PersonView:ObservableCollection
{
公共PersonView()
{
添加(newperson(){Name=“Luis”});
添加(newperson(){Name=“Gusth”});
}
}
public partial class MainWindow : Window
{
public PersonView BindThis { get; set; }
public MainWindow()
{
InitializeComponent();
BindThis = new PersonView();
}
}
<CollectionViewSource x:Key="ViewPersons" Source="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=BindThis}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Name"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
public class PersonView : ObservableCollection<Person>
{
public PersonView()
{
Add(new Person() { Name = "Luis" });
Add(new Person() { Name = "Gusth" });
}
}