c# - 数据上下文绑定在主细节场景中丢失
问题描述
我尝试通过使用 DataGrid 作为主控和 HexControl(参考: https ://github.com/abbaye/WpfHexEditorControl )作为其详细内容来构建主/详细场景。
我有一个ObservableCollection,其中RowModel类包含 DataGrid 的属性,并且我在 RowModel 类中有一个HexEditorContent类,因此当用户选择一行以在 HexEditor 控件中显示数据时,我可以创建 Master/Detail 模式。通过按钮添加新行。此外,我使用 Prism 框架。
代码中的最小示例如下所示:
MainWindow.xaml:
<Window x:Class="HexEditorTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HexEditorTest"
xmlns:hexEditor="clr-namespace:WpfHexaEditor;assembly=WPFHexaEditor"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.2*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button Content="Add Row" Click="Button_Click" Width="100"/>
<DataGrid x:Name="MainGrid" ItemsSource="{Binding Rows}" Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="A" Binding="{Binding Acol}" />
<DataGridTextColumn Header="B" Binding="{Binding Bcol}" />
</DataGrid.Columns>
</DataGrid>
<GroupBox Grid.Row="2" Header="Payload">
<hexEditor:HexEditor DataContext="{Binding ElementName=MainGrid, Path=SelectedItem.HexEditorContent}" Stream="{Binding HexByteStream}"/>
</GroupBox>
</Grid>
</Window>
及其MainWindow.xaml.cs:
public partial class MainWindow : Window
{
ViewModel viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new ViewModel();
DataContext = viewModel;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
viewModel.Rows.Add(new RowModel
{
Acol = "c",
Bcol = "d",
HexEditorContent = new HexEditorContent { HexByteStream = new MemoryStream(Enumerable.Repeat((byte)0, 10).ToArray(), true) }
});
}
}
public class ViewModel
{
public ObservableCollection<RowModel> Rows { get; set; } = new ObservableCollection<RowModel>();
}
public class HexEditorContent : BindableBase
{
private MemoryStream memStream;
public MemoryStream HexByteStream
{
get { return memStream; }
set { SetProperty(ref memStream, value); }
}
}
public class RowModel : BindableBase
{
private string aCol;
public string Acol
{
get { return aCol; }
set { SetProperty(ref aCol, value); }
}
private string bCol;
public string Bcol
{
get { return bCol; }
set { SetProperty(ref bCol, value); }
}
private HexEditorContent hexEditor;
public HexEditorContent HexEditorContent
{
get { return hexEditor; }
set { SetProperty(ref hexEditor, value); }
}
}
现在,当我单击添加新行时,新行将显示默认数据集,例如:
但是,当我添加另一行并切换回第一行时,第一行的输入消失了:
我在这里做错了什么?
每次切换时,数据上下文似乎都丢失了,并且再也没有出现......
编辑:
这是 MemoryStream 的调试视图
解决方案
推荐阅读
- javascript - 如何修复'index.js:1446警告:无法在未安装的组件上调用setState(或forceUpdate)......“在ReactJS
- angular - Angular5:构建全名时进行空检查
- python - 如何在包含至少一个下划线和大写字母的字符串中查找单词
- json - 未找到数据中心协调架构
- mysql - 我可以在 sql 中使用递归来完成这个查询吗?
- azure-devops - 子和的 DAX 表达式
- python - 如何使用 OpenCV 理解 Python 中的头部姿势估计角度?
- file - 使用 cmake 更改现有文件的权限
- kubernetes - 来自 StatefulSet 的 Pod 卡在 ContainerCreating 状态 - FailedCreatePodSandBox
- php - PHP - 无法为 PayPal REST API 设置送货地址