C#如何在gridview中编辑单元格值?

时间:2017-03-14 22:19:46

标签: c# wpf gridview

我在XAML中有一个如下所示的gridview

<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
m.myList.Add(new mylistview.myitems
        {
            id = m.id,
            something= m.something,
            currentState = m.currentState,
        });

通过将行添加到gridview中,此按钮可以完美地运行。但是,我想使用正在运行的方法修改theCurrentState。我如何找到例如ID = "8",然后修改该行的theCurrentState

显示更新后的代码 我现在已将list<Task>替换为ObservableCollection,并设法在我点击listview时将其添加到button。但是,我正在努力将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; }
    }
}

2 个答案:

答案 0 :(得分:0)

也许与

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。

答案 1 :(得分:0)

所以我看到你已经在使用数据绑定,这很好。但是你的问题让我觉得你还没有完全掌握它能为你做的一切。

我的建议是忘记将项目直接添加到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();
            }
        }
    }
}

现在gridview绑定到ObservableCollection并且该集合中保存的项可以通知感兴趣的GUI控件其属性已更改,您只需通过更改集合中的相应项即可更新GUI。 / p>

以下是使用整个技术的表单示例:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).asp

修改

我忘了你特别需要绑定到ListView的ItemSource属性。我过去的方法是在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 statesomething属性更新为excersize。他们还需要在设置值时触发OnPropertyChanged事件。