我正在开发一个项目,我希望有一个列表框,其中包含一个填充了用户数据的自定义数据模板。我的问题是,当我点击列表框中的某个项目时,如何判断我选择的项目?基本上,如果我选择“kevin”,我想显示他的数据。如果我选择Dave,我想显示他的数据。我不知道如何在绑定后获取数据......
编辑:我发现了一个非常好的教程,涵盖了这一点。一个非常隐藏的宝石。答案 0 :(得分:1)
SelectedIndex
的{{1}}属性将对应于数据源中所选项的索引。因此,假设您已绑定到IList,您应该只能使用ListBox
。我假设你不是试图支持多选,在这种情况下你可以使用相同的概念,但实现更复杂。
答案 1 :(得分:1)
将ComboBox的SelectedItem绑定到任何属性。
<Window x:Class="ComboBoxSelectedItemBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="500">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox x:Name="st"
ItemsSource="{Binding Path=Customers,Mode=TwoWay}" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding Path=SelectedCustomer,Mode=TwoWay}"
Margin="0,38,0,80">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="{Binding Path=SelectedCustomer.Name}" Grid.Column="1" VerticalAlignment="Center" Margin="5"></TextBlock>
</Grid>
public partial class Window1 : Window, INotifyPropertyChanged
{
private ObservableCollection<Customer> customers;
public ObservableCollection<Customer> Customers
{
get
{
return customers;
}
set
{
customers = value;
NotifyPropertyChanged("Customers");
}
}
private Customer selectedCustomer;
public Customer SelectedCustomer
{
get
{
return selectedCustomer;
}
set
{
selectedCustomer = value;
NotifyPropertyChanged("SelectedCustomer");
}
}
public Window1()
{
Customers = new ObservableCollection<Customer>();
Customers.Add(new Customer() { ID = 1, Name = "Ravi", Salary = 1000 });
Customers.Add(new Customer() { ID = 99, Name = "Alex", Salary = 3000 });
Customers.Add(new Customer() { ID = 123, Name = "Steve", Salary = 100 });
Customers.Add(new Customer() { ID = 31, Name = "Alice", Salary = null });
InitializeComponent();
DataContext = this;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
public class Customer:INotifyPropertyChanged
{
private int id;
public int ID
{
get
{
return id;
}
set
{
id = value;
NotifyPropertyChanged("ID");
}
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
NotifyPropertyChanged("Name");
}
}
private decimal? salary;
public decimal? Salary
{
get
{
return salary;
}
set
{
salary = value;
NotifyPropertyChanged("Salary");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
答案 2 :(得分:0)
根据您是否允许用户在ListBox中选择一个或多个项目,您可以循环所有项目并检查它是否已选中。这是一个小例子C#示例(可以在VB.NET中完成):
for (int i = 0; i < MyListBox.Items.Count; i++)
{
if(MyListBox.Items[i].Selected)
//Do what you want with the item with : MyListBox.Items[i].Value
}
答案 3 :(得分:0)
听起来你一次只能选择一个项目。如果是这样,那么我更喜欢将ListBox.SelectedItem绑定到我的ViewModel上的属性。如果我们假设您将列表框绑定到Person类的集合,那么ViewModel上的属性将是Person类型。列表框将此属性设置为所选的Person实例。
然后,只需将显示Kevin的数据的UI的其他部分绑定到ViewModel上的属性。
只要您在属性上实施了INotifyPropertyChanged,您的UI就会在您更改列表框中的所选项目时自动更新。