简体   繁体   English

ComboBox中的SelectedItem绑定

[英]SelectedItem binding in ComboBox

I have a problem with binding to SelectedItem property of ComboBox. 我绑定到ComboBox的SelectedItem属性时遇到问题。

There is an ObservableCollection, which is binded to ItemsSource property and another object field, which I want to bind to SelectedItem property, in the app. 应用程序中有一个ObservableCollection,它绑定到ItemsSource属性,另一个对象字段是我要绑定到SelectedItem属性。

But the application doesn't even start, because of Target Invocation Exception. 但是由于目标调用异常,应用程序甚至无法启动。

I don't know, if is it important to bind SelectedItem with one of the property of one instance of ItemsSource or I can use declare another property in viewmodel for it. 我不知道,将SelectedItem与ItemsSource的一个实例的属性之一绑定是否重要,或者我可以在viewmodel中使用它声明另一个属性。 I tried both variants. 我尝试了两种变体。 Didn't help. 没有帮助。 I've read some threads about such problem, but those solutions doesn't solve this. 我已经阅读了有关此类问题的一些线索,但这些解决方案无法解决此问题。

<ComboBox x:Name="CategoryComboBox"
                  ItemsSource="{Binding CategoryList}"
                  DisplayMemberPath="Name"
                  SelectedItem="{Binding SelectedCategory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                  SelectionChanged="CategoryComboBox_SelectionChanged"
                  />



public ObservableCollection<IItem> CategoryList { get; set; }

public IItem SelectedCategory
{
    get
    {
        return _selectedCategory;
    }
    set
    {
        _selectedCategory = value;
        RaisePropertyChangedEvent(nameof(SelectedCategory));
    }
}

public interface IItem
{
    int Id { get; set; }
    string Name { get; set; }
}

private void CategoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{         
    var catName = (e.AddedItems[0] as IItem).Name;
    vm.SelectedCategory = vm.CategoryList.Where(w => w.Name == catName).Select(s => s.Id).FirstOrDefault();
}

    public void LoadLanguageList()
    {            
        LanguageList = Repository.Current.GetLanguageList();
        _selectedLanguage = LanguageList.FirstOrDefault(i => i.Id == 1);
        RaisePropertyChangedEvent(nameof(SelectedLanguage));
    }

In the code upper you can see the way, how I try to bind, then the collection property, selected item property and the type of items as interface. 在代码上方,您可以看到方式,我如何尝试进行绑定,然后可以将collection属性,selected item属性和项目类型作为界面。

I know, that it's impossible to create an instance of interface, but I don't know if binding object of such type is incorrect. 我知道,不可能创建接口的实例,但是我不知道这种类型的绑定对象是否不正确。 But I tried to bind to another object type of class, which implements this interface, and the result was the same. 但是我试图绑定到另一种对象类型的类,该对象类型实现了此接口,结果是相同的。

SelectedCategory= CategoryList [0];

vm.SelectedCategory = vm.CategoryList.Where(w => w.Name == catName).FirstOrDefault();

these 2 need to be changed 这两个需要更改

Note: you don't have to create an event for SelectionChanged . 注意:您不必为SelectionChanged创建事件。 if the item is changed in ui it will automatically assign to SelectedCategory im assuming ur using MVVM so u set data context 如果在ui中更改了该项目,它将假定您使用MVVM 自动分配给SelectedCategory因此您可以设置数据上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM