Ben*_*Ben 8 .net c# silverlight wpf
我有一个组合框,它绑定到我的viewmodel(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"/>
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这一问题?
嗯,它适用于我的目的.你使用什么样的系列?


<ComboBox
SelectedItem="{Binding SelectedFoo, Mode=TwoWay}"
ItemsSource="{Binding FooCollection}">
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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>()));
Run Code Online (Sandbox Code Playgroud)
和班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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3037 次 |
| 最近记录: |