C#如何在gridview中编辑单元格值?
我在XAML中有一个gridview,如下所示C#如何在gridview中编辑单元格值?,c#,wpf,gridview,C#,Wpf,Gridview,我在XAML中有一个gridview,如下所示 <ListView x:Name="listTasks"> <ListView.View> <GridView x:Name="gridTasks"> <GridViewColumn Header="ID" HeaderStringFormat="Lowercase" Width ="26" DisplayMemberBinding="
<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>
通过将行添加到gridview
中,此按钮可以完美地工作。但是,我想使用正在运行的方法修改当前状态。例如,如何定位该行的ID=“8”
,然后修改该行的当前状态
显示更新的代码
现在,我已将我的列表
替换为可观察集合
,并在单击我的按钮时设法将其添加到我的列表视图
。但是,我正在努力将iNotifyPropertyChanged
实现到我的代码中,并使其正常工作。。。下面是我的listview类
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; }
}
}
公共类mylistview:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
私有字符串_currentState;
公共字符串当前状态
{
获取{return\u currentState;}
设置
{
_当前状态=值;
OnPropertyChanged();
}
}
公共ObservableCollection_myList=新ObservableCollection();
公共可观测集合myList
{
获取{return\u myList;}
}
私有静态int_id=0;
公共类myitems
{
公共int id{get;set;}
公共字符串something{get;set;}
公共字符串currentState{get;set;}
}
公共整数id
{
获取{return\u id;}
设置{u id=value;}
}
}
可能与
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
listoftask.Items.Cast().First(item=>item.ID==“8”)。当前状态=新闻状态;
//我不确定Cast的内容,因为我不知道ListView对其项使用什么类型
当然,您可以使用循环遍历项目,并手动检查ID。因此,我发现您已经在使用数据绑定,这很好。但你的问题让我觉得你还没有完全掌握它能为你做的一切 我的建议是不要直接将项目添加到
任务列表.items
。相反,您应该创建一个保存该列表并将listOfTasks
绑定到该列表。像这样:
ObservableCollection tasks = new ObservableCollection<mylistview.myitems>();
ListOfTasks.ItemsSource = tasks;
它应该自动更新GUI。
最后一步是确保类mylistview.myitems
实现。这比听起来容易;您只需要让它在设置属性时触发事件。大概是这样的:
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();
}
}
}
}
既然gridview绑定到一个ObservableCollection,并且该集合中的项可以通知感兴趣的GUI控件它们的属性已更改,那么您只需通过更改集合中的相应项即可更新GUI
下面是一个使用整个技术的表单示例:
编辑
我忘记了您特别需要绑定到ListView的ItemSource属性。我过去的做法是在ListView的xaml中设置ItemsSource={binding}
,然后将一个ObservableCollection分配给ListView.DataContext
。然而,我找到了一个更简单的方法,并用它更新了原来的帖子。这里有一个参考:
编辑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; }
}
我将更新当前状态
和某物
属性作为练习。他们还需要在设置其值时触发OnPropertyChanged事件。感谢您的帮助和指导。我已经添加了ObservableCollection tasks=newobservableCollection
。但是datacontext
对于gridoftask
不存在,但是对于lisoftask
它存在。我尝试了listOfTasks
,但按钮不再适用于此。有什么想法吗?这就是我在测试之前没有真正测试代码的原因。你说得完全正确,我忘记了另一个重要步骤。在列表视图的xaml上,需要添加itemssource={binding}
。我也会更新我的答案。再次感谢你花时间帮助我。非常感谢,没问题!希望这次我没有忘记任何事情,因为我不在一台可以测试新代码更改的计算机前/我发现下面这个问题与你的建议非常相似。我已经用我的代码试过了,但还没有用iNotifyPropertyChanged
部分。在没有它的情况下进行测试,数据不会添加到我的列表中,我猜这是正常的行为,因为UI没有意识到任何更改?
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();
}
}
}
}
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; }
}