简体   繁体   中英

Avoid filter textox in checkbox column in datagrid in wpf

I have one datagrid.The columns header name is binding from one xml file. Now I need to add filter text box for header template from datagrid.

  1. without textbox filter option the datagrid shown as per the below. 在此处输入图片说明

  2. After adding filter text box the datagrid as shown as below

在此处输入图片说明

I don't want to show textbox in first column ie checkbox column,how to do this? The xaml code is attached below

<UserControl .....>
    <UserControl.Resources>
        <ResourceDictionary>


            <DataGridTemplateColumn  x:Key="CustomCheckBoxTemplate">
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate >
                        <CheckBox Name="ColumnHearderCheckBox" IsThreeState="True" PreviewMouseLeftButtonDown="OnHeaderCheckBoxMouseButtonDown" />
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate >

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox PreviewMouseLeftButtonDown="OnCellCheckBoxPreviewMouseLeftButtonDown" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor,
                                      AncestorType=DataGridRow}, Path=IsSelected,Mode=TwoWay}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>


            </DataGridTemplateColumn >

             <ContextMenu x:Key="ColumnHeaderContextMenu"
                FontSize="{Binding PlacementTarget.FontSize, RelativeSource={RelativeSource Self}}">
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}" >
                        <EventSetter Event= "Click" Handler="OnContextMenuItemClicked" />
                        <Setter Property="IsCheckable" Value="True" />
                        <Setter Property="Header" Value="{Binding Header}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Visibility}" Value="Visible">
                                <Setter Property="IsChecked" Value="True"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Visibility}" Value="Collapsed">
                                <Setter Property="IsChecked" Value="False"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Visibility}" Value="Hidden">
                                <Setter Property="IsChecked" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContextMenu.ItemContainerStyle>
            </ContextMenu>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid Margin="5,0,0,0">
        <Grid.Resources>
            <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Height" Value="30"/>
                <Setter Property="Foreground" Value="WhiteSmoke"/>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}"  >
                                <Border x:Name="Border"
                             Padding="2"
                             Background="{TemplateBinding Background}"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}">
                                    <Grid x:Name="LayoutGrid">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent},
                                                 Path=ActualHeight}" />
                                        </Grid.ColumnDefinitions>
                                        <StackPanel Orientation="Vertical">
                                            <TextBlock Text="{Binding Content, RelativeSource=
                                         {RelativeSource Mode=TemplatedParent}}"/>
                                            <TextBox x:Name="txtId" Width="100"  />
                                        </StackPanel>
                                    </Grid>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>

            </Style>
            <DataTemplate x:Key="CustomTemplate">
                <StackPanel Orientation="Horizontal" >
                    <Image Source="{Binding Converter={StaticResource StringToImageConverter}}" Height="30" Width="30" Margin="30,0,0,0" HorizontalAlignment="Right"/>
                </StackPanel>
            </DataTemplate>
            <ContextMenu  x:Key="RowMenu" DataContext="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}">
                <MenuItem Header="Delete" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"  >
                </MenuItem>
                <MenuItem Header="Edit" Command="{Binding EditCommand}"/>
            </ContextMenu>
            <Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
                <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
            </Style>
        </Grid.Resources>
        <DataGrid x:Name="dataGrid"  IsReadOnly="True" ItemsSource="{Binding PatientStudyList}"  SelectionMode="{Binding SelectionMode, Converter={StaticResource SelectionModeToStringConverter}}"
                  AlternationCount="2" GridLinesVisibility="Horizontal" AutoGenerateColumns="False"  BorderThickness="1" 
                  RowStyle="{StaticResource DefaultRowStyle}" HorizontalAlignment="Stretch">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Unloaded">
                    <i:InvokeCommandAction Command="{Binding DataGridUnloadedCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
                    <Setter Property="ContextMenu" Value="{StaticResource ColumnHeaderContextMenu}" />
                    <Setter Property="Height" Value="50" />


                </Style>
            </DataGrid.ColumnHeaderStyle>
        </DataGrid>
    </Grid>
</UserControl>

You are changing the DataGridColumnHeader.Template with a new ControlTemplate

The CustomCheckBoxTemplate is providing a DataGridTemplateColumn.HeaderTemplate as DataTemplate which targets the DataGridColumnHeader.ContentTemplate . However, your ControlTemplate doesn't use the DataGridColumnHeader.ContentTemplate internally, so the HeaderTemplate part of your CustomCheckBoxTemplate will be ignored.

If it works for you, just define the DataGridColumnHeader.ContentTemplate instead of the DataGridColumnHeader.Template in your style (will behave a little differently).

<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
    ...
    <!-- Do not define Property="Template" here -->
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <!-- Similar to your ControlTemplate, but the TemplateBinding parts need to be reworked -->

Otherwise use the DataGridColumnHeader.ContentTemplate inside your ControlTemplate .

<!-- Old -->
<TextBlock Text="{Binding Content, RelativeSource=
                                     {RelativeSource Mode=TemplatedParent}}"/>
<!-- Replace by -->
<ContentPresenter
    Content="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"
    ContentTemplate="{TemplateBinding ContentTemplate}"/>

Didn't test all part, so there might still be errors in my suggestion.

Edit Actually, the last part about the ContentPresenter doesn't prevent the txtId textbox. Here is an idea where the ControlTemplate is using Triggers to change the visible parts based on the ContentTemplate property being set. Note I changed some minor details like the ColumnDefinition Width for my testing.

<ControlTemplate TargetType="{x:Type DataGridColumnHeader}"  >
    <Border x:Name="Border"
     Padding="2"
     Background="{TemplateBinding Background}"
     BorderBrush="{TemplateBinding BorderBrush}"
     BorderThickness="{TemplateBinding BorderThickness}">
        <Grid>
            <Grid x:Name="LayoutGrid"
                Visibility="Collapsed">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
                    <TextBox x:Name="txtId" Width="100"  />
                </StackPanel>
            </Grid>
            <ContentPresenter
                x:Name="TemplatedContent"
                Visibility="Visible"
                Content="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                ContentTemplate="{TemplateBinding ContentTemplate}"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="ContentTemplate" Value="{x:Null}">
            <Setter Property="Visibility" TargetName="TemplatedContent" Value="Collapsed"/>
            <Setter Property="Visibility" TargetName="LayoutGrid" Value="Visible"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM