我在XAML中有如下所示的网格视图
<ListView x:Name="listTasks">
<ListView.View>
<GridView x:Name="gridTasks">
<GridViewColumn Header="ID" HeaderStringFormat="Lowercase" Width ="26" DisplayMemberBinding="{Binding id}"/>
<GridViewColumn Header="Something" Width="113" DisplayMemberBinding="{Binding something}"/>
<GridViewColumn Header="State" Width="179" DisplayMemberBinding="{Binding currentState}"/>
</GridView>
</ListView.View>
</ListView>
我有一个按钮,它使用下面的按钮添加到这个网格视图中
m.myList.Add(new mylistview.myitems
{
id = m.id,
something= m.something,
currentState = m.currentState,
});
通过将行添加到gridview
中,此按钮完美地工作。但是,我想使用正在运行的方法修改theCurrentState
。例如,如何定位ID = "8"
,然后修改该行的theCurrentState
?
更新后的代码显示了,我现在用ObservableCollection
替换了list<Task>
,并在单击button
时设法将其添加到listview
中。但是,我很难将iNotifyPropertyChanged
实现到我的代码中,并使它正常工作.下面是我的listview class
public class mylistview : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _currentState;
public string currentState
{
get { return _currentState; }
set
{
_currentState = value;
OnPropertyChanged();
}
}
public ObservableCollection<myitems> _myList = new ObservableCollection<myitems>();
public ObservableCollection<myitems> myList
{
get { return _myList; }
}
private static int _id = 0;
public class myitems
{
public int id { get; set; }
public string something{ get; set; }
public string currentState { get; set; }
}
public int id
{
get { return _id; }
set { _id = value; }
}
}
发布于 2017-03-14 23:58:26
所以我看到你已经在使用数据绑定了,这很好。但你的问题让我觉得你还没有完全掌握它能为你做的一切。
我的建议是忘记直接向listOfTasks.Items
添加项目。相反,您应该创建一个ObservableCollection来保存该列表并将listOfTasks
绑定到它。就像这样:
ObservableCollection tasks = new ObservableCollection<mylistview.myitems>();
ListOfTasks.ItemsSource = tasks;
有了该绑定之后,当它们单击您的按钮时,您应该能够简单地将新项添加到任务列表中:
tasks.Add(new mylistview.myitems
{
id = theId,
something= something,
currentState = theCurrentState,
});
它应该自动更新GUI。最后一步是确保类mylistview.myitems
实现INotifyPropertyChanged
。这比听起来容易得多;只要设置了属性,就需要让它触发一个事件。差不多是这样的:
public class exampleProperties: INotifyPropertyChanged
{
//this is the event you have to emit
public event PropertyChangedEventHandler PropertyChanged;
//This is a convenience function to trigger the event.
//The CallerMemberName part will automatically figure out
//the name of the property you called from if propertyName == ""
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
}
}
//Any time this property is set it will trigger the event
private string _currentState = "";
public string currentState
{
get { return _currentState; }
set
{
if (_currentState != value)
{
_currentState = value;
OnPropertyChanged();
}
}
}
}
既然网格视图绑定到一个ObservableCollection,并且该集合中持有的项可以通知感兴趣的GUI控件它们的属性已经更改,那么只需更改集合中的适当项就可以简单地更新GUI。
下面是一个使用整个技术的表单示例:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).asp
编辑
我忘记了,您特别需要绑定到ItemSource属性的ListView。我过去这样做的方法是在ListView的xaml中设置ItemsSource={binding}
,然后将一个ObservableCollection分配给ListView.DataContext
。然而,我已经找到了一个更容易的方法,并更新了原来的帖子与它。这里有一个参考资料:http://www.wpf-tutorial.com/listview-control/listview-with-gridview/
编辑2
啊哈,你把iPropertyChangedNotify
加错了。它适用于myitems
类,如下所示:
public class myitems : iNotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private int _id;
public int id
{
get { return _id; }
set
{
_id = value;
OnPropertyChanged();
}
}
public string something{ get; set; }
public string currentState { get; set; }
}
我把更新current state
和something
属性作为摘录。它们还需要在设置值时触发OnPropertyChanged事件。
发布于 2017-03-14 22:42:59
也许和
listOfTasks.Items.Cast<ListViewItem>().First(item => item.ID == "8").theCurrentState = newState;
//I'm not sure about the Cast stuff, because I don't know what types the ListView uses for its items
当然,您也可以使用循环遍历条目,并手动检查ID。
https://stackoverflow.com/questions/42797636
复制相似问题