首页 > 解决方案 > 数据上下文绑定在主细节场景中丢失

问题描述

我尝试通过使用 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 的调试视图

在此处输入图像描述

标签: c#wpf

解决方案


推荐阅读