开发者

Updating DataGrid cell checkbox after editing was done in another cell of the DataGrid

I have a DataGrid bound to an ObservableCollection

    <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 is a property as following:

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

When the Translated text gets updated:

        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)) retur开发者_如何学Cn;

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

How can I also update the CheckBox to be checked off?

I tried updating the collection thinking the DataGrid will update I also tried the dgWork.Items.Refresh() command that fails with a statement this this not allowed on Edit or Add


You might need to call Commit before setting your IsTranslated property. However there is the issue that modifying another value will call RowEndEditing a million times and crash. I had a similar problem with CellEndEdit. You can prevent this by trying the following code....

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


Check to see if XLIFFData.IsTranslated raises property changed. If it does I would advise looking the XLIFFData ViewModel as you should not need a converter on the checkbox column. The ViewModel should handle updating anything related to that field unless I am misunderstanding the question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜