[英]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
的引用,并使用它来调用一个方法来查找绑定到AllStockQuoteView
的ObservableCollection
的股票代码符号,但是还没有找到一种方法以编程方式选择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.