c# - 组合框文本显示与下拉列表中的不同

标签 c# wpf xaml combobox

我想要一个带有代码和定义下拉列表的组合框,但只在文本框部分显示所选项目的定义。 For example, Y-Yes and N-No in the dropdown, and when Y is selected, only display Yes in the textbox.

最佳答案

如果您使用 WPF 来执行此操作,请使用绑定(bind)。

假设您绑定(bind)了一个类的集合:

public class Item
{
  public string Key{
    get 
    {
      return this.Value[0].ToString();
    }
  }
  public string Value{get;set;}
  public override string ToString()
  {
     return this.Key; 
  }
}

您可以使用它来显示 Key 和 Value,如图所示

<ComboBox x:Name="cmbList" ItemsSource="{Binding}" Text="{Binding SelectedItem.Value}"></ComboBox>

希望本文能帮助您解决问题。

关于c# - 组合框文本显示与下拉列表中的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3982239/

相关文章:

c# - 使用 WPF MVVM 缩放图像,将移动顶层控件。如何固定图像缩放时间?

c# - 一些文本左对齐,其余文本右对齐的 TextBlock

wpf - 简单的网格布局问题

wpf - 如何在 XAML 中对资源进行动画处理?

c# - INotifyPropertyChanged 的​​ PropertyChanged 成员始终为 null

c# - Marshal.PtrToStructure(并再次返回)和字节顺序交换的通用解决方案

c# - 使用 Xamarin.Mac 禁用耦合到 socket 的 NSMenuItem

c# - WebService 不反序列化某些对象

c# - C#中COM接口(interface)的显式接口(interface)实现

wpf - 将图像添加到资源的不同方式