WPF Datagrid cell value edit - C#

↘锁芯ラ 提交于 2019-12-25 08:28:08

问题


I have a simple datagrid with 2 columns that I populate using an observable collection. The requirement is to enable cell edit for one of the columns and then using a button, save the column data somewhere. This is how I have implemented it so far:

View Model:

 public class PlanningResult : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            public string ProducerName { get; set; }
            public string leasename { get; set; }
}

Observable Collection:

ObservableCollection<PlanningResult> populatePatternData = new ObservableCollection<PlanningResult>();
public ObservableCollection<PlanningResult> PopulatePatternData
            {
                get { return populatePatternData; }
                set
                {
                    populatePatternData = value;
                    base.OnPropertyChanged("StringList");
                }
            }

XAML : I set the "IsReadOnly=False" for the property ProducerName , hence allowing user to update this value if required.

 <DataGrid x:Name="PrintReport" ItemsSource="{Binding PopulatePatternData}" AutoGenerateColumns="False" FontFamily="Tahoma" FontSize="12" CanUserSortColumns="False"
                                                     HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch"  AlternatingRowBackground="Gainsboro"  AlternationCount="1" 
                                                     SelectionMode="Extended" SelectionUnit="Cell" >

                                            <DataGrid.Columns>
                                                    <DataGridTextColumn Header="Pattern" Binding="{Binding ProducerName}" IsReadOnly="False"  >
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style>
                                                            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                                                            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                    <DataGridTextColumn Header="Lease" Binding="{Binding leasename}" IsReadOnly="True" >
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style>
                                                            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                                                            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>

My question is what is the next step in terms of how to "get the updated values of the column (ProducerName)" and re-populate the observable collection?


回答1:


Use a TwoWay mode Binding:

Binding="{Binding ProducerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

This will update the specific item in the ObservableCollection, whenever the user changes the value of the cell.

Furthermore, use commanding in order to save the current state of your ObservableCollection. See this among many other answer and articles.



来源:https://stackoverflow.com/questions/42184614/wpf-datagrid-cell-value-edit-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!