在TreeView的SelectedItem中修改Collection中的对象

时间:2012-10-26 18:26:07

标签: .net wpf binding treeview datacontext

对象模型: 客户有很多研究,有很多科目有很多记录。树视图包含客户/研究/主题,记录在MainDataGrid中。

问题:当我修改datagrid中Record对象的双向绑定属性之一时,从不调用Record对象上的setter。我不确定如何处理这个问题,因为我仍然不了解wpf绑定模型/对象模型,以找出原因。

这不是MVVM应用程序。在代码隐藏中,在_loaded事件期间,我加载一些数据并通过一个良好实现的ObservableCollection将它分配给TreeView的datacontext。 在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>

2 个答案:

答案 0 :(得分:1)

如果要在更改TextBox中的文本后立即更改源,则需要将UpdateSourceTrigger设置为PropertyChanged。对于TextBox,defualt为LostFocus

<TextBox Text="{Binding YourBinding, UpdateSourceTrigger=PropertyChanged}"/>

答案 1 :(得分:0)

原来与this question非常相似。通过将UpdateSourceTrigger设置为LostFocus来解决此问题。

这对我来说很奇怪,因为根据MSDN on UpdateSourceTrigger

  

TwoWay或OneWayToSource的绑定侦听目标属性中的更改并将它们传播回源。这称为更新源。通常,只要目标属性更改,就会发生这些更新。这适用于复选框和其他简单控件,但它通常不适用于文本字段。每次击键后更新都会降低性能,并且在提交新值之前,它会拒绝用户退回并修复键入错误的通常机会。因此,Text属性的默认UpdateSourceTrigger值是LostFocus而不是PropertyChanged。

所以我不确定为什么会这样。