首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#如何在网格视图中编辑单元格值?

C#如何在网格视图中编辑单元格值?
EN

Stack Overflow用户
提问于 2017-03-14 22:19:46
回答 2查看 991关注 0票数 0

我在XAML中有如下所示的网格视图

代码语言:javascript
运行
复制
<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>

我有一个按钮,它使用下面的按钮添加到这个网格视图中

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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; }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-14 23:58:26

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

我的建议是忘记直接向listOfTasks.Items添加项目。相反,您应该创建一个ObservableCollection来保存该列表并将listOfTasks绑定到它。就像这样:

代码语言:javascript
运行
复制
ObservableCollection tasks = new ObservableCollection<mylistview.myitems>();
ListOfTasks.ItemsSource = tasks;

有了该绑定之后,当它们单击您的按钮时,您应该能够简单地将新项添加到任务列表中:

代码语言:javascript
运行
复制
tasks.Add(new mylistview.myitems
    {
        id = theId,
        something= something,
        currentState = theCurrentState,
    });

它应该自动更新GUI。最后一步是确保类mylistview.myitems实现INotifyPropertyChanged。这比听起来容易得多;只要设置了属性,就需要让它触发一个事件。差不多是这样的:

代码语言:javascript
运行
复制
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类,如下所示:

代码语言:javascript
运行
复制
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属性作为摘录。它们还需要在设置值时触发OnPropertyChanged事件。

票数 0
EN

Stack Overflow用户

发布于 2017-03-14 22:42:59

也许和

代码语言:javascript
运行
复制
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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42797636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档