DataGrid样式不允许打开ContextMenu

时间:2019-05-16 16:31:43

标签: c# wpf xaml wpfdatagrid

DataGrid样式不允许打开ContextMenu。建立了DataGrid的开发样式后,ContextMenu停止打开。

当我删除Style DataGrid时-上下文菜单打开-一切正常。因此,该错误是在样式中寻找。

试图在树接口DataGridRow的任何部门中替代TemplateBinding ContextMenu。不成功。

...
    <Style x:Key="Private.DataGridRow" TargetType="{x:Type DataGridRow}">
        <Setter Property="Controls:DataGridRowHelper.SelectionUnit" Value="{Binding Path=SelectionUnit, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="#d50000" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}" 
                            SnapsToDevicePixels="True">

                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                            <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                        </SelectiveScrollingGrid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
...

DataGrid的外观

            <DataGrid Grid.Column="0" 
                      ItemsSource="{Binding ItemsCustomer}"
                      Style="{StaticResource AtlantUI.DataGrid.Ghost}"
                      SelectedItem="{Binding SelectedCustomer}"
                      AutoGenerateColumns="False"
                      CanUserSortColumns="True" 
                      CanUserAddRows="False"
                      SelectionUnit="FullRow"
                      SelectionMode="Single"
                      Core:DataGridHelper.CellPadding="8 4 4 4" 
                      Core:DataGridHelper.ColumnHeaderPadding="4"
                      Margin="5,0,5,5">

使用ContextMenu。选择DataGrid行时,应打开此ContextMenu。

              <DataGrid.Resources>
                    <ContextMenu x:Key="ctx_menu" DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
                        <MenuItem Command="{Binding DataContext.RemoveCommand}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                      Header="Remove" />
                    </ContextMenu>
                </DataGrid.Resources>

                <DataGrid.ItemContainerStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Setter Property="ContextMenu" Value="{StaticResource ctx_menu}" />
                    </Style>
                </DataGrid.ItemContainerStyle>

应该如何工作。选择DataGridRow时,将打开上下文菜单。 谢谢大家。

0 个答案:

没有答案