1

I want to add a MouseBinding for a DataGrid in a global style resource, so that the "EditCommand" defined in the base ViewModel is fired for every DataGrid. The way I tried is shown below. However, the DataGrid does not show - which makes sense, as the ContentPresenter is empty. What would I have to add, to show the regular DataGrid with the InputBindings applied?

<Style x:Key="StandardTabelle" TargetType="{x:Type DataGrid}">
    <Setter Property="Margin" Value="5"/>
    <Setter Property="Background" Value="White"/>
    <Setter Property="AlternatingRowBackground" Value="#ebecec"/>
    <Setter Property="FontSize" Value="12" />
    <Setter Property="RowHeight" Value="24"/>
    <Setter Property="CanUserAddRows" Value="False"/>
    <Setter Property="CanUserDeleteRows" Value="False"/>
    <Setter Property="CanUserReorderColumns" Value="False"/>
    <Setter Property="CanUserResizeRows" Value="False"/>
    <Setter Property="CanUserResizeColumns" Value="False"/>
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <ContentPresenter>
                     <!--What goes here to show the regular content?-->
                    <ContentPresenter.InputBindings>
                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DataContext.EditCommand}" />
                    </ContentPresenter.InputBindings>
                </ContentPresenter>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Mister 832
  • 1,177
  • 1
  • 15
  • 34
  • You're targeting datagrid, then decide it's going to have a contentpresenter inside it. That ain't gonna work. https://stackoverflow.com/questions/1104601/xaml-how-to-have-global-inputbindings Ignore the heading. Use the attached property approach to add inputbindings. Or. Define a contentcontrol template that has the bindings and then use one of those with your datagrid in it when you want the inputbindings. – Andy Jan 12 '19 at 16:12

1 Answers1

2

You need to define a complete ControlTemplate. You can copy the default one by right-clicking on a DataGrid in design mode in Visual Studio and choose Edit Template->Edit a Copy, and then edit it as per your requirements, e.g.:

<Style x:Key="StandardTabelle" TargetType="{x:Type DataGrid}">
    <Setter Property="Margin" Value="5"/>
    <Setter Property="Background" Value="White"/>
    <Setter Property="AlternatingRowBackground" Value="#ebecec"/>
    <Setter Property="FontSize" Value="12" />
    <Setter Property="RowHeight" Value="24"/>
    <Setter Property="CanUserAddRows" Value="False"/>
    <Setter Property="CanUserDeleteRows" Value="False"/>
    <Setter Property="CanUserReorderColumns" Value="False"/>
    <Setter Property="CanUserResizeRows" Value="False"/>
    <Setter Property="CanUserResizeColumns" Value="False"/>
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                        <ScrollViewer.Template>
                            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                    <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1">
                                        <ScrollContentPresenter.InputBindings>
                                            <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DataContext.EditCommand}" />
                                        </ScrollContentPresenter.InputBindings>
                                    </ScrollContentPresenter>
                                    <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                    <Grid Grid.Column="1" Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                    </Grid>
                                </Grid>
                            </ControlTemplate>
                        </ScrollViewer.Template>
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

You can't just add an InputBinding to en element in the existing template in XAML without defining a complete custom template. There may be better ways of doing what you are trying to do, for example using an attached behaviour as suggested by @Andy.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you, that was, what I was looking for. I'm gonna look into the AttachedProperty approach. I guess the up- and downsides of either approach are a discussion for another day. – Mister 832 Jan 14 '19 at 17:30