Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框SelectedItem未按预期工作_C#_.net_Wpf_Silverlight - Fatal编程技术网

C# 组合框SelectedItem未按预期工作

C# 组合框SelectedItem未按预期工作,c#,.net,wpf,silverlight,C#,.net,Wpf,Silverlight,我有一个组合框,它绑定到我的viewmodel上的Foo集合(FooCollection)。我还将组合框的SelectedItem属性设置为我的viewmodel上名为SelectedFoo的Foo类型的属性 然后我设置FooCollection并选择Foo并触发相应的OnPropertyChanged事件 我的组合框包含Foo列表,但组合框中显示的项目始终是列表中的第一个项目。但是,如果您下拉组合框,则突出显示的项目是正确的项目(SelectedFoo)。因此,它选择的是正确的项目,而不是显示

我有一个组合框,它绑定到我的viewmodel上的Foo集合(
FooCollection
)。我还将组合框的
SelectedItem
属性设置为我的viewmodel上名为
SelectedFoo
Foo
类型的属性

然后我设置FooCollection并选择Foo并触发相应的OnPropertyChanged事件

我的组合框包含Foo列表,但组合框中显示的项目始终是列表中的第一个项目。但是,如果您下拉组合框,则突出显示的项目是正确的项目(
SelectedFoo
)。因此,它选择的是正确的项目,而不是显示它



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

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

嗯,它在我这方面起作用。你用的是什么样的收藏品


代码隐藏:

    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主窗口()
{
初始化组件();
DataContext=this;
FooCollection=newbindingslist();
var foo=新foo(“α”);
FooCollection.Add(foo);
foo=新foo(“Beta”);
SelectedFoo=foo;
FooCollection.Add(foo);
foo=新的foo(“伽马”);
FooCollection.Add(foo);
}
公共食物选择食物
{
获取{return(Foo)GetValue(SelectedFooProperty);}
set{SetValue(SelectedFooProperty,value);}
}
公共静态只读从属属性SelectedFooProperty=
DependencyProperty.Register(“SelectedFoo”、typeof(Foo)、typeof(MainWindow)、new UIPropertyMetadata(null));
公共绑定列表集合
{
get{return(BindingList)GetValue(FooCollectionProperty);}
set{SetValue(FooCollectionProperty,value);}
}
公共静态只读DependencyProperty FooCollectionProperty=
Register(“FooCollection”、typeof(BindingList)、typeof(MainWindow)、newUIPropertyMetadata(newbindingList());
和富班,

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
}
公共类Foo:INotifyPropertyChanged
{
公共Foo(字符串名称)
{
_名称=名称;
}
私有字符串\u名称;
公共字符串名
{
获取{return\u name;}
设置
{
if(_name==value)返回;
_名称=值;
不动产变更(“名称”);
}
}
公共重写字符串ToString()
{
返回名称;
}
#区域INotifyPropertyChanged事件
///
///在属性值更改时发生。
///
公共事件属性更改事件处理程序属性更改;
/// 
///为…引发事件
///给定的属性。
/// 
///已更改属性的名称。
受保护的无效OnPropertyChanged(字符串propertyName)
{
//验证调试生成中的属性名称
验证属性(propertyName);
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
/// 
///验证当前类是否提供具有给定
///此方法仅在调试生成中调用,并导致
///如果方法
///正在使用无效的属性名称调用。如果
///属性的名称已更改,但属性名称的参数未更改
///调用。
/// 
///已更改属性的名称。
[有条件的(“调试”)]
私有void VerifyProperty(字符串propertyName)
{
Type=GetType();
//查找具有指定名称的*public*属性
PropertyInfo pi=type.GetProperty(propertyName);
if(pi==null)
{
//没有匹配的属性-通知开发人员
字符串msg=“OnPropertyChanged已使用无效的属性名称{0}:”;
msg+=“{0}不是{1}的公共属性。”;
msg=String.Format(msg,propertyName,type.FullName);
调试失败(msg);
}
}
#端区
}

您是否碰巧看到这个问题?有帮助吗?谢谢,但那是不同的,我的类确实实现了INotifyPropertyChanged(那是我文章中OnPropertyChanged的一部分)你有没有尝试过
OneWayToSource
绑定模式?看看你的
FooCollection
SelectedFoo
是如何实现的会很有帮助,我使用的是一个列表,但根据您使用的BindingList将其更改为ObservalbleCollection(不要认为在Silverlight中可以使用该列表),我知道它工作正常。干杯