2011-05-26 121 views
8

我有一个组合框,它绑定到我的viewmodel上的Foo集合(FooCollection)。我也ComboBox的SelectedItem属性设置为一个属性上我叫SelectedFooCombobox SelectedItem按预期工作

然后我设置FooCollection和SelectedFoo Foo类型的视图模型,并触发相应的事件OnPropertyChanged。

我的组合框包含Foo列表,但显示在组合框中的项目始终是列表中的第一项。但是,如果您下拉组合框,则突出显示的项目是正确的项目(SelectedFoo)。因此,它正在选择正确的项目,但不显示它。

<ComboBox Grid.Row="5" ItemsSource="{Binding Path=FooCollection}" 
         SelectedItem="{Binding SelectedFoo, Mode=TwoWay}" 
         Name="FooSelectionControl"/> 

有谁知道如何解决这一问题?

+0

你有没有碰巧看到这个问题?它有帮助吗? http://stackoverflow.com/questions/5896006/wpf-combobox-selecteditem-binding-doesnt-work – jwismar 2011-05-26 15:53:43

+0

谢谢,但那是不同的,我的班级实施INotifyPropertyChanged(这是我的帖子的OnPropertyChanged部分) – Ben 2011-05-26 15:54:53

+0

你试过' OneWayToSource'绑定模式? – 2011-05-26 16:03:50

回答

5

嗯,它适用于我的目的。你使用什么样的收藏品?背后

​​

Screenshot 2

<ComboBox 
     SelectedItem="{Binding SelectedFoo, Mode=TwoWay}" 
     ItemsSource="{Binding FooCollection}"> 
    </ComboBox> 

代码:

public MainWindow() 
    { 
     InitializeComponent(); 

     DataContext = this; 

     FooCollection = new BindingList<Foo>(); 

     var foo = new Foo("Alpha"); 
     FooCollection.Add(foo); 

     foo = new Foo("Beta"); 
     SelectedFoo = foo; 
     FooCollection.Add(foo); 

     foo = new Foo("Gamma"); 
     FooCollection.Add(foo); 
    } 

    public Foo SelectedFoo 
    { 
     get { return (Foo)GetValue(SelectedFooProperty); } 
     set { SetValue(SelectedFooProperty, value); } 
    } 
    public static readonly DependencyProperty SelectedFooProperty = 
     DependencyProperty.Register("SelectedFoo", typeof(Foo), typeof(MainWindow), new UIPropertyMetadata(null)); 

    public BindingList<Foo> FooCollection 
    { 
     get { return (BindingList<Foo>)GetValue(FooCollectionProperty); } 
     set { SetValue(FooCollectionProperty, value); } 
    } 
    public static readonly DependencyProperty FooCollectionProperty = 
     DependencyProperty.Register("FooCollection", typeof(BindingList<Foo>), typeof(MainWindow), new UIPropertyMetadata(new BindingList<Foo>())); 

和类Foo,

public class Foo : INotifyPropertyChanged 
{ 
    public Foo(string name) 
    { 
     _name = name; 
    } 

    private string _name; 

    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      if (_name == value) return; 

      _name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 

    public override string ToString() 
    { 
     return Name; 
    } 

    #region INotifyPropertyChanged event 

    ///<summary> 
    ///Occurs when a property value changes. 
    ///</summary> 
    public event PropertyChangedEventHandler PropertyChanged; 


    /// <summary> 
    /// Raises the <see cref="PropertyChanged"/> event for 
    /// a given property. 
    /// </summary> 
    /// <param name="propertyName">The name of the changed property.</param> 
    protected void OnPropertyChanged(string propertyName) 
    { 
     //validate the property name in debug builds 
     VerifyProperty(propertyName); 

     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 


    /// <summary> 
    /// Verifies whether the current class provides a property with a given 
    /// name. This method is only invoked in debug builds, and results in 
    /// a runtime exception if the <see cref="OnPropertyChanged"/> method 
    /// is being invoked with an invalid property name. This may happen if 
    /// a property's name was changed but not the parameter of the property's 
    /// invocation of <see cref="OnPropertyChanged"/>. 
    /// </summary> 
    /// <param name="propertyName">The name of the changed property.</param> 
    [Conditional("DEBUG")] 
    private void VerifyProperty(string propertyName) 
    { 
     Type type = GetType(); 

     //look for a *public* property with the specified name 
     PropertyInfo pi = type.GetProperty(propertyName); 
     if (pi == null) 
     { 
      //there is no matching property - notify the developer 
      string msg = "OnPropertyChanged was invoked with invalid property name {0}: "; 
      msg += "{0} is not a public property of {1}."; 
      msg = String.Format(msg, propertyName, type.FullName); 
      Debug.Fail(msg); 
     } 
    } 

    #endregion 
} 
+0

嗨@tofutim,我正在使用一个List <>,但基于你使用BindingList(不认为可用在Silverlight中),并将其更改为ObservalbleCollection <>,并知道它工作正常。干杯。 – Ben 2011-05-27 08:18:18

2

也许尝试SelectedValue而不是SelectedItem。另外,请确保Foo.Equals()已正确实施。