如何从wpf中的另一个数据表中访问数据? [英] How to access data from another datatemplate in wpf?

查看:157
本文介绍了如何从wpf中的另一个数据表中访问数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Datatemplates。
一个包含一个网格,第二个包含一个按钮。
我需要发送按钮的命令参数作为选定的网格项。



我该如何做?

 < ObjectDataProvider x:Key =DatasObjectType ={x:Type ViewModel:UserControlViewModel}>< / ObjectDataProvider> 

< DataTemplate x:Key =SourceGrid>
< WPFToolKit:DataGrid x:Name =SourceDataGridItemsSource ={Binding Source = {StaticResource Datas},Path = SourceGridData}CanUserSortColumns =TrueGridLinesVisibility =NoneIsSynchronizedWithCurrentItem =TrueSelectionUnit = FullRow>< / WPFToolKit:DataGrid>
< / DataTemplate>

< DataTemplate x:Key =AddRemoveDataTemplate>
< StackPanel>
< Button Name =AddContent =Add>
< Button.Command>
< Binding Source ={StaticResource Datas}Path =AddCommand>
< / Binding>
< /Button.Command>
< Binding ElementName =SourceDataGridPath =SelectedItem>< / Binding>
< /Button.CommandParameter>
< / Button>
< StackPanel>
< / DataTemplate>


解决方案

您可以尝试使用绑定 RelativeSource 属性设置为 FindAncestor 模式,并寻找 DataGrid 对象。但是,我不知道它是否将在您的方案中工作,因为我不知道这些 DataTemplate 是如何相互关联的。是 DataGrid


$ b $中的项目使用第二个 DataTemplate 不知怎的,你的设计对我来说很奇怪。您确定在这两种情况下都需要 DataTemplate 吗?你想要实现什么?


I have 2 Datatemplates. One contain a grid , second one contain a button. I need to send command parameters of button as selected grid items.

How can i do this ?

<ObjectDataProvider x:Key="Datas" ObjectType="{x:Type ViewModel:UserControlViewModel}"></ObjectDataProvider>

<DataTemplate x:Key="SourceGrid">
<WPFToolKit:DataGrid x:Name="SourceDataGrid" ItemsSource="{Binding Source={StaticResource Datas},Path=SourceGridData}" CanUserSortColumns="True" GridLinesVisibility="None" IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow"></WPFToolKit:DataGrid>
</DataTemplate>

<DataTemplate x:Key="AddRemoveDataTemplate">
<StackPanel>
<Button Name="Add" Content="Add">
<Button.Command>
<Binding Source="{StaticResource Datas}" Path="AddCommand"> 
</Binding>
</Button.Command>
<Binding ElementName="SourceDataGrid" Path="SelectedItem"></Binding>
</Button.CommandParameter>
</Button>
<StackPanel>
</DataTemplate>

解决方案

You could try to use a Binding with the RelativeSource property set to the FindAncestor mode and looking for a DataGrid object. However, I am not sure whether it will work in your scenario because I do not know how these DataTemplates are related to each other. Is the second DataTemplate used for the items in the DataGrid?!

Somehow, your design feels strange to me. Are you sure that you need DataTemplates in both cases? What exactly do you want to achieve?

这篇关于如何从wpf中的另一个数据表中访问数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆