将Usercontrol属性绑定到Datagrid中的Button ContextMenu

时间:2017-10-05 12:13:46

标签: c# wpf data-binding datagrid user-controls

美好的一天。 我的UserControl中的Datagrid中有一个ContextMenu,它试图根据UserControl名称绑定到我的UserControl属性,但绑定不起作用。

UserControl名称

 x:Name="usercontrolManageTransferCash"

UserControl属性

public bool CanDelete
    {
        get { return (bool)GetValue(CanDeleteProperty); }
        set { SetValue(CanDeleteProperty, value); }
    }

public static readonly DependencyProperty CanDeleteProperty = DependencyProperty.Register("CanDelete", typeof(bool), typeof(ManageTransferCash), new PropertyMetadata(false));

My Binding CanDelete在ContextMenu中发送到我的UserControl名称

<DataGrid x:Name="DG_TransferDepositHis" SelectionMode="Single" 
          VerticalAlignment="Top" Margin="0,0,0,0"  FontSize="14"  >
    <DataGrid.Columns>
        <DataGridTextColumn Visibility="Collapsed" Header="ID" Binding="{Binding TransferHistoryId}" Width="auto"/>                           
        <DataGridTextColumn  Header="Date" Binding="{Binding Date, Converter={StaticResource ShortDateFormatConverter}}" MinWidth="100" Width="auto"/>
        <DataGridTextColumn  Header="Timeago" Binding="{Binding TimeAgo, Converter={StaticResource TimeAgoValueConverter}}"  Width="*"/>


        <DataGridTemplateColumn Width="auto" >
            <DataGridTemplateColumn.HeaderTemplate>
                <DataTemplate>
                    <!--<TextBlock Text="Manage"  HorizontalAlignment="Center" Margin="50,0" />-->
                </DataTemplate>
            </DataGridTemplateColumn.HeaderTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="1,0" >
                        <Button x:Name="btn_action" Content="Action" FontSize="11" Margin="3,0,3,0" 
                                Style="{DynamicResource btn-primary}" Width="65" Click="btn_action_Click"  >
                            <Button.ContextMenu>
                                <ContextMenu x:Name="bank_history_dropdown_menu" 
                                             Style="{DynamicResource MaterialDesignContextMenu}" >                                                        
                                    <MenuItem   IsEnabled="{Binding CanDelete, ElementName=usercontrolManageTransferCash}"
                                        Name="menuItem_clear"  Header="Clear"   Height="36"  Style="{StaticResource MaterialDesignMenuItem}" Click="menuItem_clear_Click" />                                                        
                                </ContextMenu>
                            </Button.ContextMenu>

                        </Button>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

我需要帮助。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您无法使用ElementName绑定到父UserControl,因为ContextMenu位于不同的元素树中。

您应该能够将Tag的{​​{1}}属性绑定到父Button,然后通过UserControl的{​​{1}}绑定到它虽然。试试这个:

PlacementTarget

答案 1 :(得分:0)

尝试使用RelativeSource表达式绑定到UserConrole CanDelete属性,例如:

IsEnabled={Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type YourNamespace.YourUserControleType}}, Path=CanDelete}

这种绑定将在XAML树中搜索“YourUserControlType”类型的元素,并且第一个元素发现它将获取其CanDelete属性并绑定到它。