简体   繁体   English

获取组合框选择项

[英]Getting combobox selected item

I'm binding to a combobox an ObservableCollection of my custom class File . 我将自定义类FileObservableCollection绑定到组合框。 I've created a particular object of type File called SelectedFile . 我创建了一个名为SelectedFile File类型的特定对象。 The problem is that in the binding showed below, SelectedFile doesn't change. 问题在于,在下面显示的绑定中, SelectedFile不会更改。 It's properly initialized, and the binding is working correctly because I can see the objects of the collection in the combobox. 它已正确初始化,并且绑定工作正常,因为我可以在组合框中看到集合的对象。

<ComboBox x:Name="combo" Width="150" Margin="5"
                              ItemsSource="{Binding LoadedFiles}"
                              SelectedItem="{Binding SelectedFile}"
                              DisplayMemberPath="Filename">

Previously I had a template for the items, but I removed it since I've read that you need additional stuff to make this work with a template. 以前,我有一个用于项目的模板,但由于已阅读到您需要其他材料才能使用该模板,因此将其删除。 Is something wrong or I'm missing something? 出问题了还是我错过了什么?

You can try to set the UpdateSourceTrigger of the SelectedItem to PropertyChanged: 您可以尝试将SelectedItem的UpdateSourceTrigger设置为PropertyChanged:

<ComboBox x:Name="combo" Width="150" Margin="5"
                          ItemsSource="{Binding LoadedFiles}"
                          SelectedItem="{Binding SelectedFile, UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="Filename">

I figured out what was happening: the initialization of the command. 我知道发生了什么:命令的初始化。 I was passing as arguments the initial (empty) selected file. 我将初始(空)选定文件作为参数传递。 I removed that and everything is working now. 我删除了它,现在一切正常。 Thanks. 谢谢。

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

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