2011-05-26 20 views
8

मेरे पास एक combobox है जो मेरे व्यूमोडेल (FooCollection) पर Foo के संग्रह से जुड़ा हुआ है। मैं भी प्रकार FooSelectedFooCombobox SelectedItem अपेक्षित के रूप में काम नहीं कर रहा है

मैं तो FooCollection और SelectedFoo सेट कहा जाता है की मेरी viewmodel की किसी प्रॉपर्टी तक बता गया की SelectedItem गुण सेट और उचित OnPropertyChanged घटनाओं आग।

मेरे combobox में फू की सूची शामिल है, लेकिन combobox में प्रदर्शित आइटम हमेशा सूची में पहला आइटम है। हालांकि, यदि आप combobox को छोड़ देते हैं, तो उस आइटम को तब हाइलाइट किया गया है जो सही आइटम (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

+0

धन्यवाद, लेकिन यह अलग है, मेरी कक्षा INotifyPropertyChanged को कार्यान्वित करती है (जो मेरी पोस्ट का ऑनप्रॉपर्टी चेंज हिस्सा था) – Ben

+0

क्या आपने ' OneWayToSource 'बाध्यकारी मोड? –

उत्तर

5

हम्म, यह मेरे अंत में काम करता है। आप किस प्रकार का संग्रह उपयोग कर रहे हैं? पीछे

Screenshot 1

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>())); 

और वर्ग फू,

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

हाय @ टोफुटिम, मैं एक सूची का उपयोग कर रहा था <> लेकिन इसे बाध्यकारी सूची (सिल्वरलाइट में उपलब्ध नहीं है) का उपयोग करके आप पर आधारित एक पर्यवेक्षण चयन <> में बदल गया है और पता है कि यह ठीक काम करता है। चीयर्स। – Ben

2

शायद चयनित इटैम के बजाय चयनित वैल्यू का प्रयास करें। साथ ही, सुनिश्चित करें कि Foo.Equals() सही ढंग से कार्यान्वित किया गया है।

संबंधित मुद्दे