C# 用对象绑定ListView
我正在尝试将ListView与TestRow类绑定,如下所示C# 用对象绑定ListView,c#,wpf,mvvm,C#,Wpf,Mvvm,我正在尝试将ListView与TestRow类绑定,如下所示 <Grid Background="White"> <ListView ItemsSource="{Binding BindingTest}" HorizontalAlignment="Left" Height="176" Margin="49,41,0,0" VerticalAlignment="Top" Width="197"> <ListView.View
<Grid Background="White">
<ListView ItemsSource="{Binding BindingTest}" HorizontalAlignment="Left" Height="176" Margin="49,41,0,0" VerticalAlignment="Top" Width="197">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
</Grid>
public ObservableCollection<TestRow> BindingTest
=> ListToObservableCollection<TestRow>.ObservableCollectionFromList(new List<TestRow>
{
new TestRow(1, "jack"),
new TestRow(2, "mark")
});
公共可观测集合绑定测试
=>ListTooObservableCollection.ObservableCollectionFromList(新列表
{
新测试行(1,“杰克”),
新测试行(2,“标记”)
});
启动后,ListView显示“XXXX.TestoRow”两次。但是,当我绑定列表时,它正在正确工作。
如何将自己的对象绑定到ListView?使用DisplayMemberPath。如果TestRow有字段:
class TestRow
{
public int Index {get;set;}
public string Name {get;set;}
}
然后使用
<ListView ItemsSource="{Binding BindingTest}" DisplayMemberPath="Name" HorizontalAlignment="Left" Height="176" Margin="49,41,0,0" VerticalAlignment="Top" Width="197">
选择的值将是TestRow