简体   繁体   English

ItemsSource 的 wpf 绑定问题

[英]wpf binding issue with ItemsSource

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

hi, I need to have the updated data in the DataGrid_RowEditEnding event, since the e.Row doesn't have the updated data, so I thought about adding <ItemsControl ItemsSource = "{Binding DataViews, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" > but it doesn't work, where am I wrong?嗨,我需要在DataGrid_RowEditEnding事件中有更新的数据,因为e.Row没有更新的数据,所以我想添加<ItemsControl ItemsSource = "{Binding DataViews, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" >但它不起作用,我哪里错了?

You will get the updated value in the CellEditEnding event handler if you just set the UpdateSourceTrigger property of the bindings to PropertyChanged .如果您只是将绑定的UpdateSourceTrigger属性设置为PropertyChanged您将在CellEditEnding事件处理程序中获得更新的值。

If you are using auto-generated columns, you could handle the AutoGeneratingColumn for the DataGrid to do this:如果您使用自动生成的列,您可以处理DataGridAutoGeneratingColumn来执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM