如何从wpf中的另一个数据模板访问数据?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用将
RelativeSource
属性设置为FindAncestor
模式的Binding
并查找DataGrid
对象。但是,我不确定它是否适用于您的场景,因为我不知道这些 DataTemplate 是如何相互关联的。第二个DataTemplate
是否用于DataGrid
中的项目?!不知何故,你的设计让我感觉很奇怪。您确定在这两种情况下都需要
DataTemplate
吗?您究竟想实现什么目标?You could try to use a
Binding
with theRelativeSource
property set to theFindAncestor
mode and looking for aDataGrid
object. However, I am not sure whether it will work in your scenario because I do not know how theseDataTemplate
s are related to each other. Is the secondDataTemplate
used for the items in theDataGrid
?!Somehow, your design feels strange to me. Are you sure that you need
DataTemplate
s in both cases? What exactly do you want to achieve?看看这篇文章。也许它会对您有所帮助:
http://www.dev102.com/2008/08/07/how-to-access-a-wpf-control-which-is- located-in-a-datatemplate/
Take a look at this article. Maybe it'll help you:
http://www.dev102.com/2008/08/07/how-to-access-a-wpf-control-which-is-located-in-a-datatemplate/