如何将输入绑定添加到DataGrid样式

时间:2019-01-12 14:55:43

标签: wpf xaml datagrid

我想在全局样式资源中为MouseBinding添加一个DataGrid,以便对每个ViewModel触发基本DataGrid中定义的“ EditCommand” 。 我尝试的方式如下所示。但是,DataGrid不显示-这很有意义,因为ContentPresenter为空。要显示应用了DataGrid的常规InputBindings,我需要添加什么内容?

<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>

1 个答案:

答案 0 :(得分:1)

您需要定义一个完整的ControlTemplate。您可以通过在Visual Studio中的设计模式中右键单击DataGrid来复制默认副本,然后选择“编辑模板”->“编辑副本”,然后根据需要对其进行编辑,例如:

<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>

您不能仅在XAML中的现有模板中的元素中添加InputBinding而不定义完整的自定义模板。可能会有更好的方法来完成您要尝试做的事情,例如,使用@Andy建议的an attached behaviour