【问题标题】:Combobox SelectedItem not working as expected组合框 SelectedItem 未按预期工作
【发布时间】:2011-05-26 15:47:25
【问题描述】:

我有一个组合框,它绑定到我的视图模型 (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"/>

有谁知道如何解决这个问题?

【问题讨论】:

  • 你有没有碰巧看到这个问题?它有帮助吗? stackoverflow.com/questions/5896006/…
  • 谢谢,但不同的是,我的班级确实实现了 INotifyPropertyChanged(那是我帖子的 OnPropertyChanged 部分)
  • 你试过OneWayToSource绑定模式吗?
  • 看看你的FooCollection和SelectedFoo是如何实现的会很有帮助。

标签: c# .net wpf silverlight


【解决方案1】:

嗯,它对我有用。您使用的是什么类型的集合?

    <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
}

【讨论】:

  • 嗨@tofutim,我使用的是 List 但根据您使用 BindingList 将其更改为 ObservalbleCollection (不要认为这在 Silverlight 中可用)并且知道它工作正常。干杯。
【解决方案2】:

也许尝试 SelectedValue 而不是 SelectedItem。另外,请确保 Foo.Equals() 正确实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2020-03-15
    • 2017-01-15
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多