Disallow button clicks the feedback button in the custom WPF ListView header

For WPF, ListView is tied to a simple list that does not need or supports sorting, how do you do this so that the user does not receive a button click when he clicks on the title? By default, clicking in the middle of a column heading looks like a button click for which nothing happens.

An example of the appearance that I am trying to achieve is in the System Control Panel> Advanced System Settings> User Profile Settings. The list of profiles does not support sorting. Accordingly, the header does not respond when clicked (except for clicks with resizing the column).

+3
source share
2 answers

You can accomplish this by setting the IsEnabled parameter to false in the column headings that you do not want to use for clicks. I inserted an example that I used as a test. The only thing I did was change the foreground brush so that the heading of the disabled column appears black, like the other heading.

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="120">
                <GridViewColumnHeader IsEnabled="True"  Content="Col A" Foreground="Black"/>
            </GridViewColumn>
            <GridViewColumn Width="120">
                <GridViewColumnHeader IsEnabled="False" Content="Col B" Foreground="Black"/>
            </GridViewColumn>
        </GridView>
    </ListView.View>
    <ListViewItem>1</ListViewItem>
    <ListViewItem>4</ListViewItem>
    <ListViewItem>2</ListViewItem>
    <ListViewItem>3</ListViewItem>
</ListView>

The first column can be clicked, the second not. Hope this helps!

edit: Sample mentioned in my comment. This method leaves the header turned on, but still does not allow it to be clicked:

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="120">
                <GridViewColumnHeader Content="Col A"/>
            </GridViewColumn>
            <GridViewColumn Width="120">
                <GridViewColumnHeader Content="Col B" PreviewMouseDown="GridViewColumnHeader_PreviewMouseDown"/>
            </GridViewColumn>
        </GridView>
    </ListView.View>
    <ListViewItem>1</ListViewItem>
    <ListViewItem>4</ListViewItem>
    <ListViewItem>2</ListViewItem>
    <ListViewItem>3</ListViewItem>

And the code for the event:

private void GridViewColumnHeader_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}

"" , . , , , . , , , 'button' " ", , .

+5

ListView, Application.xaml Application.Resources. , .

<Application>
...
    <Application.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}" >
    <Setter Property="Template" >
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                <Border BorderBrush="LightGray" BorderThickness="1,0,1,0">
                    <TextBlock Text="{TemplateBinding Content}" TextAlignment="Center" 
                               Background="LightSteelBlue"  />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

... http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/02212923-85bc-4a20-a41c-c1b558fce8c1/

+1

Source: https://habr.com/ru/post/1746010/


All Articles