对象模型:客户有很多研究有很多科目有很多记录。树视图包含客户/研究/主题,记录在 MainDataGrid 中。
问题:当我修改数据网格中 Record 对象的双向绑定属性之一时,Record 对象上的 setter 永远不会被调用。我不确定如何解决这个问题,因为我仍然对 wpf 绑定模型/对象模型了解不够,无法找出原因。
这不是 MVVM 应用程序。在代码隐藏中,在 _loaded 事件期间,我加载了一些数据并通过良好实现的 ObservableCollection 将其分配给 TreeView 的数据上下文。在 TreeView 的 SelectedItemChanged 事件中,我加载相关记录和连接属性更改的处理程序。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HPA.Studies.MCA.Client"
Title="Medra Coding Assistant" Height="800" Width="1250" MouseRightButtonDown="HandleRightClick">
<Window.Resources>
<local:HistoryTypeConverter x:Key="HistoryTypeConverter" />
<DataTemplate x:Key="MedraValuesTemplate">
<UniformGrid Columns="2" Rows="5">
<TextBlock Grid.Row="0" Grid.Column="0" Text="MedDRA LLT:" />
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding CodeValue2, Mode=TwoWay}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="MedDRA PT:" />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding CodeValue1, Mode=TwoWay}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="MedDRA HLT:" />
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding CodeValue3, Mode=TwoWay}"/>
<TextBlock Grid.Row="3" Grid.Column="0" Text="MedDRA HLGT:" />
<TextBox Grid.Row="3" Grid.Column="1" Text="{Binding CodeValue4, Mode=TwoWay}"/>
<TextBlock Grid.Row="4" Grid.Column="0" Text="MedDRA SOC:" />
<TextBox Grid.Row="4" Grid.Column="1" Text="{Binding CodeValue5, Mode=TwoWay}"/>
</UniformGrid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="8*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="3">
<TextBlock Text="Medra Coding Assistant" FontSize="15" FontWeight="Bold" />
<TextBlock Text="Available Batches:" />
</StackPanel>
<Grid x:Name="BindingRootGrid" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<TreeView x:Name="tvClients" Grid.Column="0" ItemTemplate="{StaticResource NavigatorTemplate}" ItemsSource="{Binding}">
</TreeView>
<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="6" Panel.ZIndex="1" />
<DataGrid x:Name="MainDataGrid" Grid.Column="1" AutoGenerateColumns="False"
DataContext="{Binding ElementName=tvClients, Path=SelectedItem}" ItemsSource="{Binding Path=Records}">
<DataGrid.Columns>
<DataGridHyperlinkColumn Header=" " />
<DataGridTextColumn Header="Discrim" Binding="{Binding Discriminator, Mode=OneWay}" />
<DataGridTextColumn Header="Field Name" Binding="{Binding FieldName, Mode=OneWay}" />
<DataGridTextColumn Header="Field Value" Binding="{Binding FieldValue, Mode=OneWay}" Width="160">
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTemplateColumn Header="MedDRA Value" CellTemplate="{StaticResource MedraValuesTemplate}" />
</DataGrid.Columns>
</DataGrid>
<GridSplitter Grid.Column="2" VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="6" Panel.ZIndex="1" />
<DataGrid x:Name="HistoryDataGrid" Grid.Column="2" AutoGenerateColumns="False"
DataContext="{Binding ElementName=MainDataGrid, Path=SelectedItem, Mode=OneWay}" ItemsSource="{Binding Path=HistoryList}">
<DataGrid.Columns>
<DataGridTextColumn Header="DateTime" Binding="{Binding Path=TimeStamp, Mode=OneWay}"/>
<DataGridTextColumn Header="Event Type" Binding="{Binding Path=Description, Converter={StaticResource HistoryTypeConverter}, Mode=OneWay}"/>
<DataGridTextColumn Header="Data" Binding="{Binding Path=Data, Mode=OneWay}" Width="80">
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextWrapping" Value="Wrap" />
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="User" Binding="{Binding Path=UserName, Mode=OneWay}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
<Border Grid.Row="2" Grid.ColumnSpan="3">
<TextBlock Text="Control Panel Goes here" />
</Border>
</Grid>
缺失的 TreeView ItemTemplate:
<HierarchicalDataTemplate x:Key="BatchesTemplate" ItemsSource="{Binding Formats}">
<TextBlock Text="{Binding Path=BatchName}" />
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>