【问题标题】:C# How to edit cell value in gridview?C#如何在gridview中编辑单元格值?
【发布时间】:2017-03-14 22:19:46
【问题描述】:

我在 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&lt;Task&gt;,并在我点击我的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; }
    }
}

【问题讨论】:

    标签: c# wpf gridview


    【解决方案1】:

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

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

    下面是一个使用整个技术的表单示例: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 事件。

    【讨论】:

    • 感谢您的帮助和指导。我添加了ObservableCollection&lt;mylistview.myitems&gt; tasks = new ObservableCollection&lt;mylistview.myitems&gt;。但是datacontext 不存在于gridOfTasks,但它存在于listOfTasks。我试过listOfTasks,但按钮不再适用于此。请问有什么想法吗?
    • 这就是我在测试代码之前没有实际测试代码的结果。你是绝对正确的,我忘记了另一个重要的步骤。在列表视图的 xaml 中,您需要添加 itemssource={binding}。我也会更新我的答案。
    • 再次感谢您花时间帮助我。非常感谢。
    • 没问题!希望这次我没有忘记任何事情,因为我不在一台可以测试我的新代码更改的计算机上。 ://
    • 我在下面发现了这个问题,看起来与您的建议非常相似。我已经用我的代码试过了,但还没有用 iNotifyPropertyChanged 部分。没有它进行测试,数据不会添加到我的列表中,我猜这是正常行为,因为 UI 不知道任何更改? stackoverflow.com/questions/10659347/…
    【解决方案2】:

    也许有

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-28
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多