[英]How to access the View inside the DataTemplate from Code behind
I'm binding my viewmodel and view using resource dictionary as follows 我绑定我的视图模型和使用资源字典的视图,如下所示
<DataTemplate DataType="{x:Type viewmodels:MyViewModel}">
<Views:MyView />
</DataTemplate>
in MyView, i have dataGrid x:Name="BoxDataGrid" with DataGrid.RowDetailsTemplate having other dataGrid x:Name="SpoolsDataGrid" 在MyView中,我的dataGrid x:Name =“ BoxDataGrid”与DataGrid.RowDetailsTemplate具有其他dataGrid x:Name =“ SpoolsDataGrid”
how to access MyView or datagrids above using code behind in MyViewModel ? 如何使用MyViewModel后面的代码访问上面的MyView或DataGrid?
The reason is,i want to load and show contents inside RowDetailsTemplate only when main datagrid row selected (clicked) thru event "RowDetailsVisibilityChanged". 原因是,仅当通过事件“ RowDetailsVisibilityChanged”选择(单击)主数据网格行时,我才希望在RowDetailsTemplate中加载和显示内容。
Thanks. 谢谢。
Correction: My bad. 更正:我不好。 I want to access MyView not MyViewModel
我要访问MyView而不是MyViewModel
It's quite easy. 这很容易。
DataContext
property in your MyView
object points to concrete object of MyViewModel
. MyView
对象中的DataContext
属性指向MyViewModel
具体对象。 So you can use XAML bindings to this view-model or access in code-behind eg 因此,您可以使用XAML绑定到此视图模型或在代码隐藏中访问,例如
MyViewModel model = (MyViewModel) DataContext;
asktomsk's answer is correct. asktomsk的答案是正确的。 You can access the ViewModel via the DataContext property.
您可以通过DataContext属性访问ViewModel。
However, with a little bit more effort you can almost always get around directly accessing the ViewModel from the view. 但是,稍加努力,您几乎总是可以直接从视图访问ViewModel。 The whole point of MVVM or MVC is that there aren't dependencies from View to ViewModel.
MVVM或MVC的全部要点是从View到ViewModel没有依赖关系。
Things you should research in WPF for MVVM include: 您应该在WPF for MVVM中研究的内容包括:
You need to be aware of all of these to find elegant solutions to some problems you encounter with MVVM. 您需要了解所有这些内容,才能找到针对MVVM遇到的一些问题的优雅解决方案。
You'll need to specify a bit more detail about the behaviour you are trying to get if you want us to help you figure out how you can do it without accessing the ViewModel through the datacontext. 如果您希望我们帮助您弄清楚如何做到这一点,而无需通过数据上下文访问ViewModel,则需要详细说明要尝试获得的行为。
You can, for instance, bind somethings' Visibility to a boolean in the ViewModel using a converter? 例如,您可以使用转换器将某些对象的可见性绑定到ViewModel中的布尔值?
I apologize if you know all of the above already. 如果您已经了解上述所有内容,我们深表歉意。
Just solved this problem using MVVM Light Toolkit - EventToCommand. 刚刚使用MVVM Light Toolkit-EventToCommand解决了此问题。 Other better suggestions are very much welcome.
非常欢迎其他更好的建议。
http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx
Hope this solution will be useful to others. 希望此解决方案对其他人有用。 don't need to know the view, in my datagrid view
不需要知道该视图,在我的datagrid视图中
<i:Interaction.Triggers>
<i:EventTrigger EventName="RowDetailsVisibilityChanged">
<cmd:EventToCommand Command="{Binding RowDetailsVisibilityChangedCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
and in viewmodel 在视图模型中
public RelayCommand<DataGridRowDetailsEventArgs> RowDetailsVisibilityChangedCommand
{
get;
private set;
}
and in viewmodel constructor 并在viewmodel构造函数中
RowDetailsVisibilityChangedCommand = new RelayCommand<DataGridRowDetailsEventArgs>(e =>
{
DataGrid SpoolsDataGrid = e.DetailsElement as DataGrid;
DataRowView drv = (DataRowView)e.Row.Item;
serialNo = drv.Row["BOX_SERIAL"].ToString();
SpoolsDataGrid.ItemsSource = as400Service.GetSPOOL_BY_SERIAL_NO(serialNo);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.