2011-07-02 47 views
0

我正在一个项目上工作,我想用一个自定义数据模板填充用户数据的列表框。我的问题是,当我点击列表框中的某个项目时,如何知道我选择了哪个项目?基本上,如果我选择“凯文”,我想显示他的数据。如果我选择戴夫,我想显示他的数据。我不知道如何在绑定数据后获取数据...WPF数据绑定和模板

编辑:我发现了一个非常棒的教程,涵盖了这一点。一个非常隐藏的宝石。

http://msdn.microsoft.com/en-us/library/aa480224.aspx

回答

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 
} 
+0

我实际上可以发现一个非常简单的方法。由于数据绑定到列表框,因此我可以设置列表框的属性: IsSynchronizedWithCurrentItem =“True” 然后将此设置为true,我可以将我的文本框绑定到与列表框相同的数据,并且它将填充选定的项目的数据/ – Kevin

+0

什么:代表在C#中,将在VB中实现? – Kevin

1

ListBoxSelectedIndex属性将对应于所选择的项的数据源的索引。所以假设你已经绑定了IList,你应该可以使用myDataSource[myListBox.SelectedIndex]。我假设你并没有试图支持多选,在这种情况下,你可以使用相同的概念,但实现更复杂。

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 
} 
0

这听起来像是你将只能有一次选择一个项目。如果是这样,那么我宁愿将ListBox.SelectedItem绑定到我的ViewModel上的一个属性。如果我们假设您将列表框绑定到Person类的集合,那么ViewModel上的属性将是Person类型。列表框将把这个属性设置为Person的选定实例。

然后,只需将显示Kevin数据的UI的另一部分绑定到ViewModel上的属性即可。

只要您在属性上实现了INotifyPropertyChanged,当您更改列表框中的选定项时,您的UI应该会自动更新。