如何在 C# WPF 中拥有多列下拉框?

我正在尝试在 c# wpf 中创建一个多列组合框,如下所示。有任何想法吗?

https://img3.mukewang.com/64eab2de00013ad602210184.jpg

当选择一行时,仅显示州代码,但所有详细信息都可以显示在下拉选择中。



至尊宝的传说
浏览 172回答 1
1回答

慕慕森

你可以发挥一点创意来解决这个问题。假设您有一个宽度仅为 60 像素的组合框。因此,您希望组合项显示为完整的州名称和缩写,例如CA - California,但如果选择,您只需要缩写。CA。我声明一个类来代表一个状态,如下所示:public class State{&nbsp; &nbsp; public string ShortName { get; set; }&nbsp; &nbsp; public string FullName { get; set; }&nbsp; &nbsp; private string _displayName;&nbsp; &nbsp; public string DisplayName&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _displayName = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (string.IsNullOrEmpty(_displayName))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return string.Format("{0} - {1}", ShortName, FullName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _displayName;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}技巧是您用来DisplayName在组合框中显示项目。然后,在getof中DisplayName,如果它已经有值,则返回它,如果没有,则连接状态的短名称和全名称。然后,当您进行数据绑定时,您将拥有一个状态列表以及SelectedState,并且在该属性的设置器中,您将DisplayName设为ShortName。所以,我的XAML:<Grid>&nbsp; &nbsp; <ComboBox ItemsSource="{Binding States}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SelectedValue="{Binding SelectedState}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DisplayMemberPath="DisplayName"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name="CmbStates" Width="60" Height="32"/></Grid>然后,在我的代码中:public partial class MainWindow : Window, INotifyPropertyChanged{&nbsp; &nbsp; public event PropertyChangedEventHandler PropertyChanged;&nbsp; &nbsp; protected void OnPropertyChanged(string propertyName)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));&nbsp; &nbsp; }&nbsp; &nbsp; private List<State> _states;&nbsp; &nbsp; public List<State> States&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _states; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _states = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged("States");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private State _selectedState;&nbsp; &nbsp; public State SelectedState&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _selectedState; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _selectedState = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SelectedState.DisplayName = SelectedState.ShortName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged("SelectedState");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public MainWindow()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; States = new List<State>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new State() { FullName = "California", ShortName = "CA" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new State() { FullName = "New York", ShortName = "NY" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new State() { FullName = "Oregon", ShortName = "OR" }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; DataContext = this;&nbsp; &nbsp; }}现在列表中应该有完整的串联名称:但只有选择时的缩写:
打开App,查看更多内容
随时随地看视频慕课网APP