编辑完成后,在DataGrid的另一个单元格中更新DataGrid单元格复选框

时间:2010-12-23 17:25:35

标签: wpf wpfdatagrid

我有一个绑定到ObservableCollection

的DataGrid
    <DataGrid Name="dgWork" Grid.Row="4" Grid.ColumnSpan="4" ItemsSource="{Binding Path=TranslationData}" Style="{StaticResource DataGridStyle}" Height="206"
              CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" SelectionMode="Single" VerticalScrollBarVisibility="Auto"
              RowEditEnding="dgWork_RowEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" IsReadOnly="True" />
            <DataGridTextColumn Header="Original" Binding="{Binding Path=Original}" IsReadOnly="True" CanUserSort="False" />
            <DataGridTextColumn Header="Translated" Binding="{Binding Path=Translated}" CanUserSort="False" />
            <DataGridCheckBoxColumn Header="Is Translated" Binding="{Binding Path=IsTranslated, Converter={StaticResource translatedConverter}, Mode=TwoWay}" CanUserSort="False" IsReadOnly="True" />
        </DataGrid.Columns>
    </DataGrid>

TranslationData是以下属性:

    public ObservableCollection<XLIFFData> _xliffData = new ObservableCollection<XLIFFData>();
    public ObservableCollection<XLIFFData> TranslationData { get { return _xliffData; } }

翻译文本更新后:

        DataGridRow dgRow = e.Row;
        //Update the _doc for the Row Id if original != translated
        //Update the _doc translated to equal true
        var localData = (XLIFFData) dgRow.Item;

        //Making presumption that even changing case is editing text
        if (localData.Original.Equals(localData.Translated)) return;

        foreach(var item in _xliffData)
        {
            if(item.Id == localData.Id)
            {
                item.Translated = localData.Translated;
                item.IsTranslated = "translated";
            }
        }

如何更新要检查的CheckBox?

我尝试更新集合,认为DataGrid会更新我也尝试了dgWork.Items.Refresh()命令失败并带有一个语句,这在编辑或添加时不允许

2 个答案:

答案 0 :(得分:0)

在设置IsTranslated属性之前,您可能需要调用Commit。但是,存在修改另一个值将调用RowEndEditing一百万次并崩溃的问题。我在CellEndEdit上遇到了类似的问题。您可以通过尝试以下代码来防止这种情况....

dgWork.CommitEdit()
this.Dispatcher.BeginInvoke(
    new Action(delegate()
    {
        item.IsTranslated = true; /// << Assuming this is a bool for the checkbox??
        dgWork.Items.Refresh();
    }),
    System.Windows.Threading.DispatcherPriority.Background);

答案 1 :(得分:0)

检查XLIFFData.IsTranslated是否引发属性更改。 如果是这样,我会建议查看XLIFFData ViewModel,因为您不需要复选框列上的转换器。 ViewModel应该处理更新与该字段相关的任何内容,除非我误解了这个问题。