无法在DataGrid中绑定TextBlock文本和ComboBox SelectionChanged事件

时间:2013-05-28 20:01:41

标签: wpf mvvm binding combobox textblock

我的问题是,我无法将选择更改事件的时间从ComboBox编辑到TextBlock更新。一切都显示属性但是当我在后面的代码中的SelectionChanged事件中获得Grade时,它没有用实际值更新。要获得实际值,我必须更改ComboBox上的选择,然后单击我正在编辑的数据网格的行。然后,当我选择新的成绩时,它会更新以显示最后成绩的选择。因此,即使看起来它应该全部连接,直到我点击它才会实际更新行,而不是当我选择不同的ComboBox时。那么有没有办法改变数据网格更新数据的方式以保持与ComboBox同步?任何帮助表示赞赏。感谢。

<DataGridTemplateColumn Header="Grade" CellStyle="{StaticResource StarNumberCellStyle}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Grade}" Margin="3,1,3,1">
            </TextBlock>
        </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
     <DataGridTemplateColumn.CellEditingTemplate>
         <DataTemplate>
             <ComboBox ItemsSource="{Binding DataContext.Grades, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
                 SelectedItem="{Binding Grade,Mode=TwoWay, NotifyOnTargetUpdated=True}" Tag="{Binding}" ComboBox.IsTextSearchEnabled = "True">
                 <i:Interaction.Triggers>
                     <i:EventTrigger EventName="SelectionChanged">
                         <i:InvokeCommandAction Command="{Binding DataContext.GradeSelectCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
                               CommandParameter ="{Binding}"/>
                     </i:EventTrigger>
                 </i:Interaction.Triggers>
             </ComboBox>
         </DataTemplate>
     </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

1 个答案:

答案 0 :(得分:1)

请在此处查看有关DataGridTemplateColumn警告的答案:DataGrid and Observable Collection in WPF

简而言之,这应该适合你(UpdateSourceTrigger=PropertyChanged):

<ComboBox ... 
          SelectedItem="{Binding Grade, UpdateSourceTrigger=PropertyChanged, 
                                 Mode=TwoWay, NotifyOnTargetUpdated=True}"
          ... >