【问题标题】:Trouble binding TextBlock text and ComboBox SelectionChanged Event in DataGrid在 DataGrid 中绑定 TextBlock 文本和 ComboBox SelectionChanged 事件时遇到问题
【发布时间】:2013-05-23 22:05:39
【问题描述】:

我的问题是我似乎无法将选择更改事件从 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>

【问题讨论】:

    标签: wpf mvvm binding combobox textblock


    【解决方案1】:

    在此处查看我关于 DataGridTemplateColumn 警告的回答:DataGrid and Observable Collection in WPF

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

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

    【讨论】:

    • 啊,是的,我遇到了同样的问题,需要添加该规范的文本框。谢谢这个作品。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2013-10-22
    • 2011-11-24
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多