嗯,它適用於我的目的。你使用什麼樣的收藏品?背後
<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
}
你有沒有碰巧看到這個問題?它有幫助嗎? http://stackoverflow.com/questions/5896006/wpf-combobox-selecteditem-binding-doesnt-work – jwismar 2011-05-26 15:53:43
謝謝,但那是不同的,我的班級實施INotifyPropertyChanged(這是我的帖子的OnPropertyChanged部分) – Ben 2011-05-26 15:54:53
你試過' OneWayToSource'綁定模式? – 2011-05-26 16:03:50