首页 > 解决方案 > DataGrid 中的 RichTextBox 更新 (mvvm)

问题描述

我有一个在单元格中有几个 RichtextBox 的 DataGrid(因为需要对数据进行格式化)。任何人都可以帮助我尝试更新属性更改的来源吗?到目前为止,我一直无法找到解决方案。

这是我目前要做的;

扩展 RichTextBox 以添加 Document.FlowDocument 作为 DependencyProperty;

    public class BindableRTBox : RichTextBox
{
   
    public static readonly DependencyProperty DocumentProperty =
    DependencyProperty.Register("BindableDocument", typeof(FlowDocument), typeof(BindableRTBox),
       new FrameworkPropertyMetadata
       {
           BindsTwoWayByDefault = true,
           PropertyChangedCallback = new PropertyChangedCallback(OnDocumentChanged)
       }
     );


    public FlowDocument BindableDocument
    {
        get {return (FlowDocument)GetValue(DocumentProperty);}
        set { SetValue(DocumentProperty, value);}
    }

    

    public DependencyProperty UpdateBinding(RichTextBox richText)
    {
        SetValue(DocumentProperty, richText.Document); 
        return DocumentProperty;      
    }

 
    private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Debug.WriteLine("****DOCUMENT CHANGED CALLED*****");
        var thisControl = (RichTextBox)d;
        thisControl.Document = (e.NewValue == null) ? new FlowDocument() : (FlowDocument)e.NewValue;
    }
}

我为此使用 Calburn Micro,因此目前使用 GotFocus 事件来设置 CommandBindings 以格式化控件(粗体等)。然后基于此处的示例。使用 FocusLost 尝试更新 BindableRTXBox BindableDocument 依赖项。

Xaml

<DataGrid HeadersVisibility="Column" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="False" cal:Message.Attach="[Event SelectionChanged] = [Action UpdateGridSelection($source, $eventArgs)]; 
                          [Event CellEditEnding] = [Action CellEditAction($source, $eventArgs)]; [Event BeginningEdit] = [Action CellEditData($source, $eventArgs)]" 
                          ColumnHeaderStyle="{StaticResource HeaderStyle}" x:Name="riskDataGovGrid" ItemsSource="{Binding Path=_riskDataGov}">     
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Reg" FontWeight="Bold" Binding="{Binding Path=Reg}" MaxWidth="32" MinWidth="32" CanUserSort="False"/>
                        <DataGridTemplateColumn Header="Description" Width="*" MinWidth="150"  >
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                   <control:BindableRTBox FontWeight="Normal"   BorderThickness="0" Background="Transparent"
                                                           cal:Message.Attach="[Event GotFocus] = [Action Editor_SelectedText($source)];
                                                                               [Event LostFocus] = [Action UpdateBinding($source)]"  
                                                           BindableDocument="{Binding Path=Description, Converter={StaticResource FlowConverter}, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/> 
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Controls in Place Impact"  >
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                   //another BindableRTBox
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        </DataGrid.Columns>
                        </DataGrid>

ViewModel 中的 UpdateBinding 调用此 BindableRTBox 类中的 UpdateBinding(传递 TextTextbox 值)。

DataGrid 的 ItemSource 与 BindableCollection 相关。

我遇到的问题是将 BindableDocuemnt 设置为 UpdateSourceTrigger PropertyChanged 会使编辑操作停止工作。使用 LostFocus - 编辑操作不会得到更新,而且我也在努力想出一种方法让 RichTextBox 通过更新失去焦点推送。

任何想法都将受到欢迎!

标签: mvvmwpf-controlsdependency-properties

解决方案


推荐阅读