2011-05-26 65 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()已正確實施。