我已经对这个主题做了很多阅读,我越来越感到沮丧和困惑,在没有MVVM的情况下,这样的简单事情很难做到。
我将列表设置为:
cboLender.ItemsSource = lenders;
源列表是包含Lender
个对象的自定义类型列表。其属性如:ID
和CompanyName
。
然后以编程方式将其设置为:
var selectedLender = lenders.SingleOrDefault(x => x.ID == _someId);
cboLender.SelectedItem = selectedLender;
cboLender.SelectedValuePath = "CompanyName";
cboLender.SelectedValue = selectedLender.CompanyName;
//cboLender.DisplayMemberPath = "CompanyName";
在代码中,项目设置正确成为SelectedItem
,但在UI上,组合框为空。所以看起来已经选择了 没有 。我也尝试了显示成员路径属性,但这也不起作用。
这是一个很简单的问题,但我现在一直在尝试和研究我们现在需要帮助。
答案 0 :(得分:1)
试试此代码
LenderList = new ObservableCollection<Lender>();
Lender l = new Lender();
l.CompanyName = "11212dffdd";
l.ID = "1";
LenderList.Add(l);
cboLender.ItemsSource = LenderList;
cboLender.DisplayMemberPath = "CompanyName";
var selectedLender = LenderList.SingleOrDefault(x => x.ID == "1");
cboLender.SelectedItem = selectedLender;
cboLender.SelectedValuePath = "CompanyName";
cboLender.SelectedValue = selectedLender.CompanyName;
代码背后的
namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region Inotify
/// <summary>
/// Property Change Event
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Property Change Method
/// </summary>
/// <param name="prop">Property Name</param>
internal void RaisePropertyChanged(string prop)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
#endregion
#region Constructor
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
}
#endregion
#region Properties
private ObservableCollection<Lender> lenderList;
public ObservableCollection<Lender> LenderList
{
get { return lenderList; }
set
{
lenderList = value;
RaisePropertyChanged("LenderList");
}
}
#endregion
#region Events
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LenderList = new ObservableCollection<Lender>();
Lender l = new Lender();
l.CompanyName = "11212dffdd";
l.ID = "1";
LenderList.Add(l);
cboLender.ItemsSource = LenderList;
cboLender.DisplayMemberPath = "CompanyName";
var selectedLender = LenderList.SingleOrDefault(x => x.ID == "1");
cboLender.SelectedItem = selectedLender;
cboLender.SelectedValuePath = "CompanyName";
cboLender.SelectedValue = selectedLender.CompanyName;
}
#endregion
}
public class Lender : INotifyPropertyChanged
{
#region Inotify
/// <summary>
/// Property Change Event
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Property Change Method
/// </summary>
/// <param name="prop">Property Name</param>
internal void RaisePropertyChanged(string prop)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
#endregion
private string companyName;
public string CompanyName
{
get { return companyName; }
set
{
companyName = value;
RaisePropertyChanged("CompanyName");
}
}
private string id;
public string ID
{
get { return id; }
set
{
id = value;
this.RaisePropertyChanged("ID");
}
}
}
}
在xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cef="clr-namespace:WpfApplication1"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" x:Name="testWindow" Height="500" Loaded="Window_Loaded" Width="300" >
<Grid >
<StackPanel x:Name="stk">
<ComboBox x:Name="cboLender" Width="100"/>
</StackPanel>
</Grid>
</Window>
答案 1 :(得分:0)
我认为您应该将 ComboBox 的 SelectedIndex 设置为0.默认值为-1。