问题描述
|
我有一个组合框,它绑定到我的视图模型(
FooCollection
)上的Foo集合。我还将组合框的SelectedItem
属性设置为类型为Foo
的视图模型上称为SelectedFoo
的属性。
然后,我设置FooCollection和SelectedFoo并触发适当的OnPropertyChanged事件。
我的组合框包含Foo的列表,但是组合框中显示的项目始终是列表中的第一项。但是,如果下拉组合框,则突出显示的项目是正确的项目(SelectedFoo
)。因此,它选择正确的项目,但不显示它。
<ComboBox Grid.Row=\"5\" ItemsSource=\"{Binding Path=FooCollection}\"
SelectedItem=\"{Binding SelectedFoo,Mode=TwoWay}\"
Name=\"FooSelectionControl\"/>
有谁知道如何解决这一问题?
解决方法
嗯,对我有用。您正在使用哪种收藏?
<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>),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
}
,也许尝试使用SelectedValue而不是SelectedItem。另外,请确保正确实现了Foo.Equals()。