Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

gridview - prevent wpf listview header double click autosizes column

I have a listview where I have templated the column headers and the listview items are templated also. However I have different tempalates for some of the rows in the grid view. When a user double clicks on the list view column header where you can drag the width of the column, the column header will auto resize, meaning it will increase its size. This causes a problem for me because my column header width is no longer in sync with the width of the columns in my row templates.

Is there a quick and easy way to prevent this double click behavior on the header of a column?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, set up a double-click handler on the ListView itself. Then in the handler, use code like this:

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (TryFindParent<GridViewColumnHeader>(e.OriginalSource as DependencyObject) != null)
        e.Handled = true;
}

Where TryFindParent is defined as:

public static T TryFindParent<T>(DependencyObject current) where T : class
{
    DependencyObject parent = VisualTreeHelper.GetParent(current);

    if (parent == null) return null;

    if (parent is T) return parent as T;
    else return TryFindParent<T>(parent);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...