【问题标题】:How do i edit the "ComboBoxItem"?如何编辑“ComboBoxItem”?
【发布时间】:2017-05-05 12:06:00
【问题描述】:

我的项目中有以下ComboBox,但是如果我这样写的话,有件事让我非常困扰:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="170" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Label Content="Candidate" Margin="3" Foreground="White" Grid.Column="0"/>
    <ComboBox ItemsSource="{Binding CandidateList, UpdateSourceTrigger=PropertyChanged}" 
              SelectedItem="{Binding Candidate, UpdateSourceTrigger=PropertyChanged}"
              IsEditable="True" Margin="3" Grid.Column="1">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} {1}">
                            <Binding Path="FirstName"/>
                            <Binding Path="SecondName"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>

因此,展开版本中的名称格式正确,但在 ComboBox 本身中显示不正确...

有没有可能在 xaml 中进行这项工作?

【问题讨论】:

    标签: c# wpf mvvm combobox


    【解决方案1】:

    只是一个想法,但您可以通过添加带有 getter 的 DisplayName 属性(需要将 OnPropertyChanged("DisplayName") 添加到您的 First/第二个名称设置器),或通过覆盖候选类的 ToString() 方法。基本上 -

    Override ToString()
    {
        return FirstName + " " + SecondName ;
    }
    

    实际上我不确定如果您更改值是否会由 OnPropertyChanged 触发:/

    【讨论】:

    • 看看它是否会在你的属性发生变化时更新..不确定它是否会
    【解决方案2】:

    以前有人问过这个问题。

    WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item

    WPF Combobox: Different template in textbox and drop-downlist

    您可以向数据对象添加一个返回全名的属性,并将 ComboBox 的 TextSearch.TextPath 属性设置为此属性,或者尝试修改 ComboBoxItem 容器的 ControlTemplate。请查看以上链接了解更多信息。

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 2018-11-26
      相关资源
      最近更新 更多