mvvm - 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 通过更新失去焦点推送。
任何想法都将受到欢迎!
解决方案
推荐阅读
- linux - Linux find -type f 被忽略
- ios - 使用 UISearchController 时,iOS 13 UIBarButtonItem 不可点击且 UINavigationBars 重叠
- mysql - mysql-connector AttributeError:“NoneType”对象没有属性“fetchall”
- c# - 如何检查包含反斜杠的字典中键的值
- powershell - 将凭证传送到 Invoke-WebRequest
- python - 使用 .loc[row_indexer,col_indexer] = value 更新列时收到警告
- java - java - 如何通过java mysql驱动程序从sql查询字符串中获取固定值?
- php - 如何在 Laravel 中根据数据库查询结果检查复选框?
- autodesk-forge - 如何在使用 2 条腿客户端凭据身份验证创建视图时结合 3 腿身份验证在 Forge Viewer 中查看模型
- database - 函数不会在所有代码路径上返回值