XAML:
<ListView ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
<GridViewColumn DisplayMemberBinding="{Binding FirstName}" Header="FirstName" />
<GridViewColumn DisplayMemberBinding="{Binding Test}" Header="Test" />
</GridView>
</ListView.View>
</ListView>
CS:
public ObservableCollection<Person> Persons { get; private set; }
private string _test;
public string Test
{
get { return _test; }
set { _test = value; RaisePropertyChanged("Test"); }
}
public MainViewModel()
{
this.Persons = new ObservableCollection<Person>
{
new Person
{
Name = "Doe",
FirstName = "John",
DateOfBirth = new DateTime(1981, 9, 12)
},
new Person
{
Name = "Black",
FirstName = "Jack",
DateOfBirth = new DateTime(1950, 1, 15)
}
};
Test = "TestTestTestTestTest";
}
如何正确绑定值(Test)?ListView是UserControl的一部分,我试图绑定到下面的语法,但它仍然无效。
{Binding Test , RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type viewModel:MainViewModel}}}