【问题标题】:Updating Values in the Control Template of the ComboBox更新组合框控件模板中的值
【发布时间】:2014-08-15 17:09:29
【问题描述】:

我在包含组合框的数据网格中实现了一个列。为了在列表仅包含一个值时显示文本框而不是组合框,我使用了这篇文章中的解决方案:

How to hide combobox toggle button if there is only one item?

但是,当列表中的那个值发生变化时,它不会在文本框中更新。当然,我已经实现了 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>

【问题讨论】:

  • 显示你试过的代码
  • 你去。我没有发布代码,因为它与我发布的链接中的完全一样。

标签: c# wpf combobox


【解决方案1】:

我可以看到您正在绑定到项目本身,而不是其中的任何属性

所以也许您需要绑定到数据项的相应属性

例如

<TextBlock Text="{Binding Items[0].MyProperty, ElementName=CList}" />

假设您的预期属性是MyProperty

注意如果没有基础属性,那么您必须删除该项目并再次将新项目添加到列表中才能更新文本块,在这种情况下 INotifyPropertyChanged 也将不起作用

【讨论】:

  • 我没有任何基础属性,因此在更新我绑定到的列表时,我覆盖了它两次,一次是空列表,然后是包含新值的列表。我想列表中项目的计数必须更改才能更新文本框中的该值。非常感谢。
  • 是的,你是对的。似乎您正在绑定到字符串列表。我建议绑定可观察的字符串集合,这样您就可以简单地在集合上使用 .ClearItems() 或 .RemoveItem(0) 后跟 .Add("new item") 而不是替换整个列表两次
猜你喜欢
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 2021-05-11
  • 2013-02-03
相关资源
最近更新 更多