班级客户-
public class Client:INotifyPropertyChanged
{
private string _ClientName;
public string ClientName
{
get { return _ClientName; }
set { _ClientName = value;
OnPropertyChanged("ClientName");
}
}
private string _ClientID;
public string ClientID
{
get { return _ClientID; }
set { _ClientID = value;
OnPropertyChanged("ClientID");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Called when [property changed].
/// </summary>
/// <param name="PropertyName">Name of the property.</param>
private void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
CodeBehind-
List<Client> clients = GetClients();
ComboBox1.ItemsSource = clients;
public List<Client> GetClients()
{
List<Client> lst=new List<Client>();
Client obj = new Client();
obj.ClientName = "asd";
obj.ClientID = "1";
lst.Add(obj);
return lst;
}
查看 Xaml-
<ComboBox Name="ComboBox1" Width="100" Height="30" DisplayMemberPath="ClientName" SelectedValuePath="ClientID"/>