如何从wpf中的另一个数据模板访问数据?

发布于 2024-09-03 04:09:18 字数 964 浏览 2 评论 0原文

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

我该怎么做?

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

已下线请稍等 2024-09-10 04:09:18

您可以尝试使用将 RelativeSource 属性设置为 FindAncestor 模式的 Binding 并查找 DataGrid 对象。但是,我不确定它是否适用于您的场景,因为我不知道这些 DataTemplate 是如何相互关联的。第二个 DataTemplate 是否用于 DataGrid 中的项目?!

不知何故,你的设计让我感觉很奇怪。您确定在这两种情况下都需要 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文