2010-08-17 132 views
1

问题:谁能请提供完整的代码示例,说明一个怎样编程方式更改数据绑定WPF组合框的的SelectedItem不使用MyComboBox.SelectedIndex?如何编程设置数据绑定WPF组合框的SelectedItem?

代码示例:这是我目前有。

XAML:

<Window x:Class="Wpf.ComboBoxDemo.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <ComboBox Name="MyComboBox" DisplayMemberPath="LastName" SelectedIndex="0"/> 
    </Grid> 
</Window> 

代码隐藏:

using System.Collections.ObjectModel; 
using System.Windows; 

namespace Wpf.ComboBoxDemo 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      ObservableCollection<Person> myPersonList = new ObservableCollection<Person>(); 

      Person personJobs = new Person("Steve", "Jobs"); 
      Person personGates = new Person("Bill", "Gates"); 

      myPersonList.Add(personJobs); 
      myPersonList.Add(personGates); 

      MyComboBox.ItemsSource = myPersonList; 

      // How do I programmatically select the second Person, i.e. "Gates"? 
      // The best pratice must be to somehow to set something like IsCurrentlySelected on the model, so the view update automatically. But how? 
      MyComboBox.SelectedIndex = 1; // This works, but is there no way without using the index? 

     } 

     private class Person 
     { 
      public string FirstName { get; set; } 
      public string LastName { get; set; } 

      public Person(string firstName, string lastName) 
      { 
       FirstName = firstName; 
       LastName = lastName; 
      } 
     } 
    } 
} 

类似的问题:我当然在网上搜索了第一,但没有发现任何帮助了我。

  • 更改内部视图模型一个枚举绑定组合框的的SelectedItem(MSDN
  • 编程设置组合框的SelectedItem在WPF(3.5SP1)(Stack Overflow

回答

1

在我的头顶(我可能是错误的),使窗口实现INotifyPropertyChanged并添加事件:

namespace Wpf.ComboBoxDemo 
{ 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     public MainWindow() 
     { 

然后添加一个属性为当前选择的项目,其通知上的变化:

 private Person _selected; 
     public Person MySelected 
     { 
      get { return _selected; } 
      set 
      { 
       if (value != _selected) 
       { 
        _selected = value;    
        if (PropertyChanged != null) 
        { 
         PropertyChanged(this, 
          new PropertyChangedEventArgs("MySelected")); 
        } 
       } 
      } 
     } 

现在绑定组合框(绑定可能是更先进的在这里使用FindAncestor但有时为了简单起见,我把DataContext的在后面的代码):

XAML

 <ComboBox 
      Name="MyComboBox" 
      DisplayMemberPath="LastName" 
      SelectedItem="{Binding MySelected}" /> 

后面的代码

public MainWindow() 
    { 
     InitializeComponent(); 
     // ... 

     // this will cause the "MySelected" binding to target the correct property on this object 
     MyComboBox.DataContext = this; 
    } 

我认为它会类似的东西。我现在不能测试它,但希望它能让你朝正确的方向发展。

编辑:如果你想尝试绑定继承人的“其他方式”如何。展开SelectedItem结合是这样的:

 <ComboBox 
      Name="MyComboBox" 
      DisplayMemberPath="LastName" 
      SelectedItem="{Binding MySelected, 
       RelativeSource={RelativeSource FindAncestor, 
        AncestorType={x:Type Window}}}" /> 

现在,您可以跳过后面的代码设置DataContext

public MainWindow() 
    { 
     InitializeComponent(); 
     // ... 

     // this will cause the "MySelected" binding to target the correct property on this object 
     //MyComboBox.DataContext = this; 
    } 

这是因为FindAncestor模式使得ComboBox自己找对象,其属性它应该有约束力,而不是你特别说明。

当前的热点话题在这里办公是这两种方式是最好的。对我来说,它只是更多的XAML和更少的代码后面(或者反过来),只需要使用地方的代码,其中的youre舒适的工作方法。我认为有些情况下后者是首选的(比如当你在其他控件中包含数据绑定控件时),但是我只是在讨论,所以我还没有真正弄清楚那些部分。

+0

伟大的微调,非常感谢您的帮助! – Lernkurve 2010-08-17 11:39:10

+0

很高兴我能帮到你。 :)我添加了第二种绑定方法,如果你想尝试一下。该代码没有经过测试,所以我希望我还有一些运气。 – Mizipzor 2010-08-17 11:56:54

+0

现在我在this.DataContext = myPersonlist(它不需要ComboBox有一个名称)和FindAncestor方法之间撕裂,它根本不需要代码隐藏。 – Lernkurve 2010-08-17 14:34:53

相关问题