【问题标题】:wpf binding issue with ItemsSource与 ItemsSource 的 wpf 绑定问题
【发布时间】:2020-12-22 20:09:53
【问题描述】:
<ItemsControl ItemsSource="{Binding DataViews}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <DataGrid MaxHeight="500" VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch" Margin="0,0,10,0" MaxColumnWidth="450"
                                          RowStyle="{StaticResource DataGridRowSql}" Style="{StaticResource DataGridStyleSQL}"
                                          ColumnHeaderStyle="{StaticResource StyleDataGridColumnHeaderDefault}" ItemsSource="{Binding}"
                                          IsReadOnly="{Binding RelativeSource={RelativeSource AncestorType=Page},Path=Locked}"
                                          RowEditEnding="DataGrid_RowEditEnding" >
                                    <DataGrid.CommandBindings>
                                        <CommandBinding Command="Copy" Executed="CommandBinding_Executed"></CommandBinding>
                                    </DataGrid.CommandBindings>
                                    <DataGrid.InputBindings>
                                        <KeyBinding Key="C" Modifiers="Ctrl" Command="Copy"></KeyBinding>
                                    </DataGrid.InputBindings>
                                </DataGrid>
                                <GridSplitter Background="Red" Height="10" HorizontalAlignment="Stretch" ResizeDirection="Rows" ResizeBehavior="PreviousAndNext"></GridSplitter>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel VerticalAlignment="Stretch"></StackPanel>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>

嗨,我需要在DataGrid_RowEditEnding 事件中更新数据,因为e.Row 没有更新数据,所以我想添加&lt;ItemsControl ItemsSource = "{Binding DataViews, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" &gt; 但它不起作用,我在哪里错了吗?

【问题讨论】:

  • 如果您在数据直接输入数据网格时尝试验证数据,您会发现大量边缘情况。我通常的做法是使数据网格成为只读,用户在不同的面板覆盖中编辑所选项目。
  • 不幸的是它是从数据库查询派生的数据网格,我必须通过数据网格
  • 你应该检查this question,因为它似乎是你要找的。​​span>

标签: wpf binding datagrid


【解决方案1】:

如果您只是将绑定的UpdateSourceTrigger 属性设置为PropertyChanged,您将在CellEditEnding 事件处理程序中获得更新的值。

如果您使用自动生成的列,您可以处理 AutoGeneratingColumnDataGrid 来执行此操作:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    DataGridBoundColumn dataGridBoundColumn = e.Column as DataGridBoundColumn;
    if (dataGridBoundColumn != null)
    {
        dataGridBoundColumn.Binding = new Binding(e.PropertyName) { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
    }
}

【讨论】:

  • 您知道无法从 xaml 执行此操作吗?
  • 您无法使用样式设置绑定的属性,并且您正在使用自动生成列。
猜你喜欢
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 2011-10-26
相关资源
最近更新 更多