【问题标题】:WPF Datagrid cell value edit - C#WPF Datagrid 单元格值编辑 - C#
【发布时间】:2017-02-12 05:46:35
【问题描述】:

我有一个包含 2 列的简单数据网格,我使用可观察的集合填充这些数据网格。要求是为其中一列启用单元格编辑,然后使用按钮将列数据保存在某处。到目前为止,这就是我实现它的方式:

查看模型:

 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; }
}

可观察的集合:

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

XAML:我为属性 ProducerName 设置了“IsReadOnly=False”,因此允许用户在需要时更新此值。

 <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>

我的问题是下一步如何“获取列的更新值 (ProducerName)”并重新填充 observable collection

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    使用双向模式绑定:

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

    每当用户更改单元格的值时,这将更新 ObservableCollection 中的特定项目。

    此外,使用命令来保存 ObservableCollection 的当前状态。请参阅this 以及许多其他答案和文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多