更新组合框的控制模板中的值
本文关键字:控制 组合 更新 | 更新日期: 2023-09-27 18:10:15
我在包含组合框的数据网格中实现了一个列。为了在列表只包含一个值时显示文本框而不是组合框,我使用了这篇文章中的解决方案:
如何隐藏组合框切换按钮,如果只有一个项目?
但是,当列表中的一个值被更改时,它不会在文本框中更新。当然,我已经实现了INotifyPropertyChanged,只要我在列表中有多个项目(换句话说,当组合框显示时),它就可以工作,但在TextBlock中的值永远不会更新。
编辑: <DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="CList" ItemsSource="{Binding Values, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="0" BorderBrush="Transparent"
Background="Transparent">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Items.Count, ElementName=CList}" Value="1">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock Text="{Binding Items[0], ElementName=CList}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
我可以看到你是绑定到项目本身,而不是它的任何属性
所以你可能需要绑定到你的数据项的属性
如
<TextBlock Text="{Binding Items[0].MyProperty, ElementName=CList}" />
假设您的预期属性是MyProperty
注意如果没有底层属性,那么您将不得不删除项目并再次添加新的列表,以便更新文本块,在这种情况下INotifyPropertyChanged也将不起作用