简体   繁体   English

如何以编程方式从viewmodel中选择listview项?

[英]How can I programatically select a listview item from within the viewmodel?

I have a MVVM application with a MainWindowViewModel connected to a MainWindow view. 我有一个MVVM应用程序,其MainWindowViewModel连接到MainWindow视图。
On the MainWindow view I have a ComboBox containing stock ticker symbols. MainWindow视图中,我有一个包含股票代码符号的ComboBox

I have another viewmodel and view called AllStockQuoteViewModel connected to AllStockQuoteView which contains a list of stocks and their prices. 我有另一个视图模型和名为AllStockQuoteViewModel视图连接到AllStockQuoteView ,其中包含股票及其价格列表。

I want to be able to select an item from the ComboBox and have the item in the AllStockQuoteView selected and highlighted. 我希望能够从ComboBox选择一个项目,并选中并突出显示AllStockQuoteView的项目。 On my MainWindowViewModel I have saved the reference to the AllStockQuoteViewModel and use it to call a method to find the stock ticker symbol in the ObservableCollection bound to the AllStockQuoteView , but haven't found a way to programmatically select the item on the AllStockQuoteView . 在我的MainWindowViewModel我已经保存了对AllStockQuoteViewModel的引用,并使用它来调用一个方法来查找绑定到AllStockQuoteViewObservableCollection的股票代码符号,但是还没有找到一种方法以编程方式选择AllStockQuoteView上的AllStockQuoteView

I have a SelectedQuote property on the AllStockQuoteViewModel bound to the listview on the AllStockQuoteView and I can select one of the items and my SelectedQuote property is set fine. 我有一个SelectedQuote对物业AllStockQuoteViewModel势必在列表视图AllStockQuoteView ,我可以选择的项目之一,我SelectedQuote属性设置罚款。 If I set this programmatically in my SelectQuote method, it doesn't appear as if the item is selected in the view, although the item is passed back to the MainWindowViewModel and I can use it to populate the textblocks on MainWindow view. 如果我在我的SelectQuote方法中以编程方式设置它,它看起来好像在视图中选择了该项,尽管该项目被传递回MainWindowViewModel并且我可以使用它来填充MainWindow视图上的文本块。

I'd like to be able to show the item on the AllStockQuoteView as being selected via highlighting as if the user selected it. 我希望能够通过突出显示选择AllStockQuoteView上的项目,就像用户选择它一样。

How can this be done? 如何才能做到这一点?

Its very easy to implement 它很容易实现

You need two things in your view model A List of your objects and a selected item property 在视图模型中需要两件事物对象列表和选定的项目属性

        private CustomObject _selectedCustomObject;

        public ObservableCollection<CustomObject> CustomObjects
        {
            get
            {
                return new ObservableCollection<CustomObject>();
            }
        }

        public CustomObject SelectedCustomObject
        {
            get { return _selectedCustomObject; }
            set
            {
                if (_selectedCustomObject== value)
                {
                    return;
                }

                _selectedCustomObject= value;
                PropertyChanged.Raise(this, x => x.SelectedCustomObject);
            }
        }

Then in your view you add your List/Combo control and bind to both properties. 然后在您的视图中添加List / Combo控件并绑定到这两个属性。

<ListView ItemsSource="{Binding CustomObjects}"
          SelectedItem="{Binding SelectedCustomObject}">

Then all you need to do is set the viewmodel properties and the view will update. 然后,您需要做的就是设置viewmodel属性,视图将更新。

First you have to think about your model and the whole MVVM approach, a good starting point is http://blogs.msdn.com/b/kashiffl/archive/2010/11/14/mvvm-technical-description.aspx . 首先,您必须考虑您的模型和整个MVVM方法,一个很好的起点是http://blogs.msdn.com/b/kashiffl/archive/2010/11/14/mvvm-technical-description.aspx

After you can implement your functionality by different ways, one would be to implement something like the Observer Pattern or you try to use methods like Notify Property-Changed-Events . 在以不同方式实现功能之后,可以实现Observer Pattern之类的功能,或者尝试使用Notify Property-Changed-Events之类的方法

Hope i was able to help, 希望我能够提供帮助,

Greetings 问候

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

相关问题 如何以编程方式单击ListView项? - How Can I Click a ListView Item Programatically? 如何从ViewModel将已编辑图像添加到ListView? - How I can add edited images to ListView from a ViewModel? 如何通过 ViewModel 将 Listview 中的项目文本传输到新页面? - How can I transfer the text of an item in a Listview to a new page through the ViewModel? 如何在 Listview 中再次选择所选项目? - How can I select the selected item again in the Listview? 如何使用 XAML 在 Xamarin.Forms 的 ListView 中选择一个项目 - How can I select an item in a ListView in Xamarin.Forms with XAML 如何从列表视图中查看一个项目,当我单击列表视图的该项目时,它将在文本块上显示数据? - How can i view an item from a listview that when I click on that item of the listview it will show data on a textblock? 如何在listView中选择项目并在richTextBox中显示所选项目的内容? - How can i select item in listView and display the selected item content in richTextBox? 如何在 selectedindexchanged 事件中从列表视图中选择项目? - How to select item from listview in selectedindexchanged event? 如何以编程方式更改Better ListView Item的值? - How to change value of Better ListView Item programatically? 如何将listview item绑定绑定到页面的Viewmodel? - How to bind listview item binding to Viewmodel of the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM