我是C# WPF
的新用户,并在DataGrid
中自定义了C# WPF
,我在下面创建了subclass
: -
namespace DataGridFiltter
public class FilteringDataGrid : Microsoft.Windows.Controls.DataGrid
{
.......
}
}
并在xaml
文件中我正在使用此类,如下所示: -
<UserControl x:Class="formGroup"
xmlns:dataGridFiltter="clr-namespace:DataGridFiltter">
<dataGridFiltter:FilteringDataGrid IsFilteringCaseSensitive="False" x:Name="groupTable" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontSize="16" AutoGenerateColumns="False" ItemsSource="{Binding DataCollection, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="groupTable_SelectionChanged" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.IsSharedSizeScope="True" AlternatingRowBackground="#FFD6D1D1" CanUserAddRows="false" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" IsReadOnly="True" Margin="0,0,-8,0" >
<DataGrid.ItemBindingGroup>
<BindingGroup/>
</DataGrid.ItemBindingGroup>
<DataGrid.Columns>
<DataGridTextColumn Header="RecordNumber" Width="100" Visibility="Hidden" />
<DataGridTextColumn Header="NAME" Width="200" Binding="{Binding GroupName}" />
<DataGridTextColumn Header="Group Name" Width="144" Binding="{Binding ParentGroupName}" />
<DataGridTextColumn Header="Creation Time" Width="200" Binding="{Binding CreationTime}" />
<DataGridTextColumn Header="Modification Time" Width="200" Binding="{Binding ModificationTIME}" />
<DataGridTextColumn Header="User" Width="100" Binding="{Binding PeopleName}" />
</DataGrid.Columns>
</dataGridFiltter:FilteringDataGrid>
我使用的风格如下: -
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:dataGridFiltter="clr-namespace:DataGridFiltter">
<dataGridFiltter:HeaderFilterConverter x:Key="headerConverter"/>
<Style TargetType="{x:Type my:DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type my:DataGridColumnHeader}">
<ControlTemplate.Resources>
<Storyboard x:Key="ShowFilterControl">
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
<DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="Transparent"/>
<SplineColorKeyFrame KeyTime="00:00:00.5000000" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="HideFilterControl">
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00.4000000" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(UIElement.OpacityMask).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="Black"/>
<SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#00000000"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<my:DataGridHeaderBorder x:Name="dataGridHeaderBorder" Margin="0" VerticalAlignment="Top" Height="61" IsClickable="{TemplateBinding CanUserSort}" IsHovered="{TemplateBinding IsMouseOver}" IsPressed="{TemplateBinding IsPressed}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" SortDirection="{TemplateBinding SortDirection}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.ColumnSpan="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="grid" Width="Auto" Grid.Row="0" Height="Auto" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- <Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="10" />
</Grid.RowDefinitions> -->
<ContentPresenter x:Name="contentPresenter"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}">
<ContentPresenter.Content>
<MultiBinding Converter="{StaticResource headerConverter}">
<MultiBinding.Bindings>
<Binding ElementName="filterTextBox" Path="Text" />
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content" />
</MultiBinding.Bindings>
</MultiBinding>
</ContentPresenter.Content>
</ContentPresenter>
</Grid>
<Grid Grid.Row="1">
<TextBox x:Name="filterTextBox" HorizontalAlignment="Right" MinWidth="25" Height="Auto" OpacityMask="Black" Visibility="Visible" Text="" TextWrapping="Wrap" Grid.ColumnSpan="1"/>
</Grid>
</Grid>
</my:DataGridHeaderBorder>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!-- <Trigger.EnterActions>
<BeginStoryboard x:Name="ShowFilterControl_BeginStoryboard" Storyboard="{StaticResource ShowFilterControl}"/>
<StopStoryboard BeginStoryboardName="HideFilterControl_BeginShowFilterControl"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard x:Name="HideFilterControl_BeginShowFilterControl" Storyboard="{StaticResource HideFilterControl}"/>
<StopStoryboard BeginStoryboardName="ShowFilterControl_BeginStoryboard"/>
</Trigger.ExitActions> -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我收到DataGrid.ItemBindingGroup
,DataGrid.Columns
和DataGridTextColumn
有人可以帮助我使用上面代码的正确方法,因为我创建了subclass
的{{1}}。