简体   繁体   English

WPF 列表框绑定,自动 select 从视图 Model 中选择的项目

[英]WPF listbox binding , auto select selected item from viw Model

I have a listbox and a modelview, which I used to bind listbox items from a obervable collection.我有一个列表框和一个模型视图,用于绑定来自可观察集合的列表框项。

My requirement is that I need to set the selectedItem as well as selectedIndex from view model, so the selected item can be visible to the user.我的要求是我需要从视图 model 中设置 selectedItem 和 selectedIndex,以便用户可以看到所选项目。

Hope somebody have solution for me希望有人为我提供解决方案

Make a SelectedItem property in your view model after that you can write something like this在您的视图 model 中创建一个 SelectedItem 属性,之后您可以编写类似这样的内容

SelectedItem="{Binding SelectedItem,UpdateSourceTrigger=PropertyChanged}"

in your XAML.在您的 XAML 中。

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

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