简体   繁体   English

如何在xaml中绑定包含元素

[英]how to bind the containing element in xaml

I have a RadGridView which has a context menu and in this context menu I have a button which I set a command on in the parameters I want to pass the gridview itself - how do I do this? 我有一个RadGridView ,它带有一个上下文菜单,在这个上下文菜单中,我有一个按钮,可以在要传递gridview本身的参数中设置一个命令-我该怎么做?

<telerik:RadGridView Height="620" HorizontalAlignment="Right" Name="radGridView2" VerticalAlignment="Top" Width="1564" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" ItemsSource="{Binding Collections}" BorderBrush="#FFD7D2D2" FontFamily="Mangal" FontSize="14" Loaded="radGridView2_Loaded" AutoGenerateColumns="False"
                             RowStyleSelector="{StaticResource rowStyleSelector}" GroupRenderMode="Flat" ShowColumnSortIndexes="True" CanUserFreezeColumns="False" CanUserResizeColumns="False"
                             DataContext="{Binding}" IsReadOnly="True" DataLoading="radGridView2_DataLoading" DataLoaded="radGridView2_DataLoaded" GroupPanelForeground="#FFF01D1D"
                             ShowColumnFooters="True" ShowGroupFooters="True" AutoExpandGroups="True" GridLinesVisibility="Both" Grid.Row="1" Margin="0,0,35,0" MouseDoubleClick="radGridView2_MouseDoubleClick" 
                             ColumnWidth="52*">
            <telerik:RadGridView.ContextMenu>
                <ContextMenu>
                    <ContextMenu.Items>
                        <Button Command="{Binding PrintCommand}" CommandParameter="{Binding}">
                            <Button.Content>
                                <TextBlock Text="Print"/>
                            </Button.Content>
                        </Button>
                    </ContextMenu.Items>
                </ContextMenu>
            </telerik:RadGridView.ContextMenu>
         </telerik:RadGridView>

Using {Binding} as my CommandParameter does not work it gives me the row in which I click - the data context of that row - which is some business object - how do I get the containing grid. {Binding}用作CommandParameter不起作用,它给我单击的行-该行的数据上下文-这是一些业务对象-如何获取包含网格。

If you mean with 'containing grid' the RadGridView try 如果您的意思是“包含网格”, RadGridView尝试RadGridView

CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}"

You will need the PlacementTarget property because ContextMenu has its own VisualTree. 您将需要PlacementTarget属性,因为ContextMenu具有自己的VisualTree。

使用RelativeSource绑定到父元素数据上下文:

CommandParameter="{Binding DataContext, RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM