Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

WPFのDataGridの左上を押しても全選択したくない

$
0
0

というときどうするか。ControlTemplateをいじりましょう(完

WPFのいいところはControlTemplateで、完全にコントロールの見た目をカスタマイズする余地が残されてるという点ですが、WPFのプロパティの範囲でできないカスタマイズとかが出てきたらControlTemplateを差し替えるとかいう、いきなりハードル高い感じになってしまうところですね。ということで、こういうControlTemplate当てればOKです。

<Style x:Key="DataGridStyle1"TargetType="{x:Type DataGrid}"><Setter Property="Background"Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /><Setter Property="Foreground"Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /><Setter Property="BorderBrush"Value="#FF688CAF" /><Setter Property="BorderThickness"Value="1" /><Setter Property="RowDetailsVisibilityMode"Value="VisibleWhenSelected" /><Setter Property="ScrollViewer.CanContentScroll"Value="true" /><Setter Property="ScrollViewer.PanningMode"Value="Both" /><Setter Property="Stylus.IsFlicksEnabled"Value="False" /><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><!-- Original --><!--<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}}}" />                                    --><!-- 変更後 --><Button 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 AncestorT<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"                                                            CanContentScroll="{TemplateBindingCanContentScroll}"                                                            Grid.ColumnSpan="2"                                                            Grid.Row="1" /><ScrollBar x:Name="PART_VerticalScrollBar"                                               Grid.Column="2"                                               Maximum="{TemplateBindingScrollableHeight}"                                               Orientation="Vertical"                                               Grid.Row="1"                                               Visibility="{TemplateBindingComputedVerticalScrollBarVisibility}"                                               Value="{BindingVerticalOffset, Mode=OneWay, RelativeSource={RelativeSourceTemplatedParent}}"                                               ViewportSize="{TemplateBindingViewportHeight}" /><Grid Grid.Column="1"                                          Grid.Row="2"><Grid.ColumnDefinitions><ColumnDefinition Width="{BindingNonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSourceAncestorType={x:TypeDataGrid}}}" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions><ScrollBar x:Name="PART_HorizontalScrollBar"                                                   Grid.Column="1"                                                   Maximum="{TemplateBindingScrollableWidth}"                                                   Orientation="Horizontal"                                                   Visibility="{TemplateBindingComputedHorizontalScrollBarVisibility}"                                                   Value="{BindingHorizontalOffset, Mode=OneWay, RelativeSource={RelativeSourceTemplatedParent}}"                                                   ViewportSize="{TemplateBindingViewportWidth}" /></Grid></Grid></ControlTemplate></ScrollViewer.Template><ItemsPresenter SnapsToDevicePixels="{TemplateBindingSnapsToDevicePixels}" /></ScrollViewer></Border></ControlTemplate></Setter.Value></Setter><Style.Triggers><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsGrouping"                           Value="true" /><Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping"                           Value="false" /></MultiTrigger.Conditions><Setter Property="ScrollViewer.CanContentScroll"                    Value="false" /></MultiTrigger></Style.Triggers></Style>

長いですね。途中にあるコメントのあるところのButtonにSelectAllのコマンドがバインドされてるのが全選択機能を有効化してるので、そのコマンドを外してやる感じです。


Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>