Стиль DataGrid не позволяет открывать ContextMenu - PullRequest
1 голос
/ 16 мая 2019

Стиль DataGrid не позволяет открывать ContextMenu.Установив разработанный стиль для DataGrid, ContextMenu перестал открываться.

Когда я удаляю Style DataGrid - открывается контекстное меню - все работает правильно.Поэтому ошибку ищем в стиле.

Попытка заменить TemplateBinding ContextMenu в любом отделе древовидного интерфейса DataGridRow.Неудачно.

...
    <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.Это ContextMenu должно открываться при выборе строки DataGrid.

              <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 открывается контекстное меню. Спасибо всем.

...