组合框 SelectedIndex MVVM WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22070049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ComboBox SelectedIndex MVVM WPF
提问by Hardgraf
I have a ComboBox bound to an ObservableCollection of tbPublicationswhich populates as it should. I then select a row from a DataGrid which fires another Create form in which I insert a new record into tbPublications, all good.
我有一个 ComboBox 绑定到一个 ObservableCollection,tbPublications它应该填充。然后我从 DataGrid 中选择一行,它会触发另一个 Create 表单,我在其中插入一条新记录tbPublications,一切都很好。
When I close said create form and return to my ComboBox form I am clearing and re-reading in the one new item to my ObservableCollection, returning the user to the item they've just created. The ComboBox then displays the one item from my newly populated collection, all good.
当我关闭所说的创建表单并返回到我的 ComboBox 表单时,我正在清除并重新读取我的一个新项目ObservableCollection,将用户返回到他们刚刚创建的项目。ComboBox 然后显示我新填充的集合中的一项,一切都很好。
My problem is that on returning to my ComboBox form, the new publication is not set as selected item in the ComboBox display, the user has to click the ComboBox then select the item.
我的问题是,返回到我的 ComboBox 表单时,新出版物未设置为 ComboBox 显示中的选定项目,用户必须单击 ComboBox 然后选择该项目。
I can't use SelectedIndex = "0"in my view XAML as I want to show the whole ObservableCollectionin my ComboBox on page load.
我不能SelectedIndex = "0"在我的视图中使用XAML,因为我想ObservableCollection在页面加载时在我的 ComboBox 中显示整体。
Is there any way to use a method in the ViewModel maybe to solve this issue, something maybe such as..
有没有什么办法可以在 ViewModel 中使用一个方法来解决这个问题,比如..
private void SetSelectedIndex()
{
if (MyObservableCollection.Count == 1)
{
//Set selected indexer to "0";
}
}
Found a solution to this, not sure if it's the cleanest 'MVVM' solution...
找到了解决方案,不确定它是否是最干净的“MVVM”解决方案......
After reading in my ObservableCollection I invoke this method:
在我的 ObservableCollection 中阅读后,我调用了这个方法:
if (_ModelPublicationsObservableList.Count == 1)
{
SelectedPublication = _ModelPublication;
SetSelectedIndex();
}
Here's the method which gets the current main window and sets the SelectedIndex:
这是获取当前主窗口并设置 SelectedIndex 的方法:
private void SetSelectedIndex()
{
ArticlesDataGridWindow singleOrDefault = (ComboBoxWindow)Application.Current.Windows.OfType<ComboBoxWindow>().SingleOrDefault(x => x.IsLoaded);
singleOrDefault.comboBox1.SelectedIndex = 0;
}
回答by Ramashankar
Did you consider using the SelectedItem property of combobox? You can bind the selected item property of combobox to get or set the selected item.
您是否考虑使用组合框的 SelectedItem 属性?你可以绑定combobox的selected item属性来获取或者设置选中的item。
XAML
XAML
<ComboBox ItemsSource="{Binding Path=Publications}" SelectedItem="{Binding Path=SelectedPublication, Mode=TwoWay}" />
ViewModel
视图模型
public class ItemListViewModel
{
public ObservableCollection<Publication> Publications {get; set;}
private Publication _selectedPublication;
public Publication SelectedPublication
{
get { return _selectedPublication; }
set
{
if (_selectedPublication== value) return;
_selectedPublication= value;
RaisePropertyChanged("SelectedPublication");
}
}
}
If you want to set the selected item from View model,You can set the SelectedPublicationproperty as-
如果要从 View 模型中设置所选项目,可以将SelectedPublication属性设置为-
SelectedPublication = Publications[0];
Or you can locate the required item in the Publicationscollection and assign it to SelectedPublicationproperty.
或者,您可以在Publications集合中找到所需的项目并将其分配给SelectedPublication属性。
回答by Inga
Add UpdateSourceTrigger=PropertyChangedto your binding.
将UpdateSourceTrigger=PropertyChanged添加 到您的绑定中。
SelectedItem={Binding Path=SelectedPublication, Mode=TwoWay}, UpdateSourceTrigger=PropertyChanged}

