直接xmal style:
<Style x:Key="GridStyle" TargetType="DataGrid"> <Setter Property="BorderBrush" Value="Gray"></Setter> <Setter Property="BorderThickness" Value="0,1"></Setter> <Setter Property="AutoGenerateColumns" Value="False"></Setter> <Setter Property="ScrollViewer.CanContentScroll" Value="True"></Setter> <Setter Property="GridLinesVisibility" Value="Horizontal"></Setter> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"></Setter> <Setter Property="SelectionMode" Value="Single"></Setter> <Setter Property="SelectionUnit" Value="FullRow"></Setter> <Setter Property="CanUserDeleteRows" Value="False"></Setter> <Setter Property="CanUserSortColumns" Value="False"></Setter> <Setter Property="CanUserReorderColumns" Value="False"></Setter> <Setter Property="IsReadOnly" Value="True"></Setter> <Setter Property="CanUserResizeColumns" Value="False"></Setter> <Setter Property="CanUserResizeRows" Value="False"></Setter> <Setter Property="CanUserAddRows" Value="False"></Setter> <Setter Property="Background" Value="DarkGray"></Setter> <Setter Property="Foreground" Value="White"></Setter> <Setter Property="CellStyle"> <Setter.Value> <Style TargetType="DataGridCell"> <Setter Property="BorderThickness" Value="0"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="DataGridCell"> <Border BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="Silver"></Setter> </Trigger> <Trigger Property="IsSelected" Value="true"> <Setter Property="Background" Value="SlateGray"></Setter> </Trigger> </Style.Triggers> </Style> </Setter.Value> </Setter> <Setter Property="ColumnHeaderStyle"> <Setter.Value> <Style TargetType="DataGridColumnHeader"> <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="HorizontalAlignment" Value="Center"></Setter> </Style> </Setter.Value> </Setter> <Setter Property="RowStyle"> <Setter.Value> <Style TargetType="DataGridRow"> <Setter Property="Height" Value="45"></Setter> <Setter Property="Background" Value="Gray"></Setter> <Setter Property="Foreground" Value="White"></Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="Silver"></Setter> </Trigger> <Trigger Property="IsSelected" Value="true"> <Setter Property="Background" Value="SlateGray"></Setter> </Trigger> </Style.Triggers> </Style> </Setter.Value> </Setter> </Style>
使用:
<DataGrid Style="{StaticResource GridStyle}" ItemsSource="{Binding Path=Pacses}" > <DataGrid.Columns> <DataGridTextColumn Header="Name" Width="3*" MinWidth="120" Binding="{Binding Name}" /> <DataGridTextColumn Header="IP" Width="3*" MinWidth="120" Binding="{Binding IP}" /> <DataGridTextColumn Header="PORT" Width="3*" MinWidth="120" Binding="{Binding Port}"/> <DataGridTemplateColumn Header="#" Width="4*" MinWidth="160"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <ToggleSwitchButton IsChecked="{Binding IsDefault}" ToolTip="Set Default" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=SelectedItem}" Command="{Binding ElementName=PacsSettingPage,Path=DataContext.SetDefaultPacsCmd}"> </ToggleSwitchButton> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>