【发布时间】:2019-01-11 20:48:10
【问题描述】:
public MainWindow()
{
InitializeComponent();
listView1.ItemsSource = myData; // myData is not ObservableCollection
}
private void AddData_ButtonClick(object sender, RoutedEventArgs e)
{
myData.Add(/*adding new item*/);
listView1.Items.Refresh();
}
和xml代码:
<ListView x:Name="listView1" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="File Name"/>
<GridViewColumn DisplayMemberBinding="{Binding NumOfPoints}" Header="Points"/>
</GridView>
</ListView.View>
</ListView>
效果很好,我只是想改进我的代码。例如删除 Items.Refresh() 方法并改进绑定。 我是 wpf 的新手,正在寻找最佳实践建议,我现在不喜欢 mvvm。 任何建议将不胜感激。
【问题讨论】:
-
myData是什么类型,如果它属于 ObservableCollection 那么我相信你不必使用Refresh() -
不,它不是 ObservableCollection
-
List<SDRfile> files = new List<SDRfile> { new SDRfile("file1", "ok1", 11), new SDRfile("file2","ok2",22)}; -
` 类 SDRfile { public string Name { get;放; } 公共字符串状态 { 获取;放; } 公共 int NumOfPoints { 得到;放; } 公共 SDRfile(字符串名称,字符串状态,int numOfPoints){ 名称 = 名称;状态=状态;点数 = 点数; } }`
标签: wpf listview data-binding