简体   繁体   English

如何从后面的代码访问DataTemplate内部的视图

[英]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中研究的内容包括:

  • Attached Properties 附属物业
  • Attached Behaviors 附带行为
  • Mediators 调解员
  • Value Converters 价值转换器
  • Markup Extensions 标记扩展

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.

相关问题 如何在代码隐藏中从DataTemplate内部的TextBlock获取文本 - How to get the Text from TextBlock inside a DataTemplate in code-behind 如何从DataTemplate获取代码中的元素 - How to get element in code behind from DataTemplate 如何在C#背后的代码中访问datatemplate内部的网格控件名称 - how to access the grid control name inside the datatemplate in code behind c# DataTemplate 中的按钮如何注册并监听后面代码中按钮的加载事件,但无法访问数据上下文? - How can a button inside of a DataTemplate register and listen to a loaded event of the button in the code behind, but can't access the datacontext? 如何访问后面代码中的元素,该元素在Xamarin.Foms的xaml页面中的DataTemplate中声明? - How to get access the element in code behind, which is declared inside a DataTemplate in xaml page in Xamarin.Foms? 在后面的代码中访问DataTemplate控件 - Access DataTemplate controls in code behind C#-来自后面代码的WPF访问DataTemplate中的元素 - C# - WPF Access elements in a DataTemplate from code behind 如何通过代码隐藏在DataTemplate下访问按钮? - How to access button under DataTemplate via code behind? 从后面的代码创建DataTemplate - Creating DataTemplate from code behind 如何在后面的代码中从DataTemplate创建控件实例 - How to create instance of control from DataTemplate in code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM