DataGrid列头中添加CheckBox控件, HeaderSyle 在资源文件中,给模板中的控件添加事件
下面是资源文件
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Style x:Key="DataGridColumnHeaderCheckBox" TargetType="sdk:DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="10.5" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="SeparatorBrush" Value="#FFDFE3E6" />
<Setter Property="Padding" Value="4,4,5,4" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridColumnHeader">
<Grid x:Name="Root">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:0.1" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Duration="00:00:00.001" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0" Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Duration="00:00:00.001" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
<SplineDoubleKeyFrame KeyTime="0" Value=".1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SortStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:0.1" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Unsorted" />
<VisualState x:Name="SortAscending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
<DoubleAnimationUsingKeyFrames Duration="00:00:00.00" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0" Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Duration="00:00:00.00" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
<SplineDoubleKeyFrame KeyTime="0" Value=".1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SortDescending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
<DoubleAnimation Storyboard.TargetName="SortIconTransform" Storyboard.TargetProperty="ScaleY" Duration="0" To="-.9" />
<DoubleAnimationUsingKeyFrames Duration="00:00:00.00" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0" Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Duration="00:00:00.00" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Offset)">
<SplineDoubleKeyFrame KeyTime="0" Value=".1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="Background" Opacity="0.65" Grid.ColumnSpan="3" Grid.RowSpan="3">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1.4" StartPoint="0.5,0">
<GradientStop Color="{StaticResource PrimaryColor}" Offset="0.75" />
<GradientStop Color="{StaticResource SecondaryColor}" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Border CornerRadius="0,0,40,40" x:Name="Highlight" RenderTransformOrigin="0.5,1" Grid.ColumnSpan="3">
<Border.Background>
<RadialGradientBrush>
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.25" ScaleY="2" />
<TranslateTransform Y="-0.6" />
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop Color="#BFFFFFFF" Offset="0" />
<GradientStop Color="#4CFFFFFF" Offset="1" />
</RadialGradientBrush>
</Border.Background>
</Border>
<ContentPresenter Grid.RowSpan="2" Content="{TemplateBinding Content}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" />
<Rectangle x:Name="VerticalSeparator" Grid.RowSpan="2" Grid.Column="2" Width="1" VerticalAlignment="Stretch" Fill="{TemplateBinding SeparatorBrush}" Visibility="{TemplateBinding SeparatorVisibility}" />
<Path Grid.RowSpan="2" x:Name="SortIcon" Fill="{TemplateBinding Foreground}" RenderTransformOrigin=".5,.5" HorizontalAlignment="Left" VerticalAlignment="Center" Opacity="0" Grid.Column="1" Stretch="Uniform" Width="8" Data="F1 M -5.215,6.099L 5.215,6.099L 0,0L -5.215,6.099 Z ">
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="SortIconTransform" ScaleX=".9" ScaleY=".9" />
</TransformGroup>
</Path.RenderTransform>
</Path>
<CheckBox x:Name="cbAll" HorizontalAlignment="Center" Margin="0" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" Grid.ColumnSpan="3" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在pager页中添加样式,并给CheckBox控件注册事件
xmal文件中datagrid
<sdk:DataGrid x:Name="dataGrid" AutoGenerateColumns="False" MinHeight="100" Width="Auto"
IsReadOnly="True" Height="Auto" HorizontalContentAlignment="Center">
<sdk:DataGrid.Columns><!--HeaderStyle="{StaticResource DataGridColumnHeaderUnSelectedWithCheckBox}"-->
<sdk:DataGridTemplateColumn HeaderStyle="{StaticResource DataGridColumnHeaderCheckBox}" Width="35">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<CheckBox x:Name="cb" HorizontalAlignment="Center" VerticalContentAlignment="Center" Click="cb_Click"> </CheckBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="角色名称" Binding="{ Binding RoleName }" MinWidth="160" ></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="角色描述" Binding="{ Binding Description}" MinWidth="160"></sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
cs代码
#region DataGrid全选Chebox处理
//注册checkbox全选事件
void dataGrid_Loaded(object sender, RoutedEventArgs e)
{
CheckBox cb = GetCheckBoxWithParent(this.dataGrid, typeof(CheckBox), "cbAll");
if (cb != null)
{
cb.Checked += new RoutedEventHandler(cbAll_Checked);
cb.Unchecked += new RoutedEventHandler(cbAll_Checked);
}
}
//找到模板中的checkbox控件
private CheckBox GetCheckBoxWithParent(UIElement parent, Type targetType, string CheckBoxName)
{
if (parent.GetType() == targetType && ((CheckBox)parent).Name == CheckBoxName)
{
return (CheckBox)parent;
}
CheckBox result = null;
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
if (GetCheckBoxWithParent(child, targetType, CheckBoxName) != null)
{
result = GetCheckBoxWithParent(child, targetType, CheckBoxName);
break;
}
}
return result;
}
private void cbAll_Checked(object sender, RoutedEventArgs e)
{
CheckBox chk = sender as CheckBox;
bool check = chk.IsChecked.Value;
if (check)
{
foreach (RoleInfo item in RoleList)
{
this.SelectedList.Add(item);
chk = this.dataGrid.Columns[0].GetCellContent(item) as CheckBox;
if (chk != null)
chk.IsChecked = true;
}
}
else
{
foreach (RoleInfo item in RoleList)
{
chk = dataGrid.Columns[0].GetCellContent(item) as CheckBox;
if (chk != null)
chk.IsChecked = false;
}
this.SelectedList.Clear();
}
}
void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
CheckBox chk =this.dataGrid.Columns[0].GetCellContent(e.Row) as CheckBox;
chk.IsChecked = false;
chk.Click += new RoutedEventHandler(cb_Click);
}
private void cb_Click(object sender, RoutedEventArgs e)
{
CheckBox chk = sender as CheckBox;
bool check = chk.IsChecked.Value;
RoleInfo p = chk.DataContext as RoleInfo;
if (check)
{
if (!this.SelectedList.Contains(p))
SelectedList.Add(p);
}
else
{
SelectedList.Remove(p);
}
}
#endregion DataGrid全选Chebox处理