Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 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 Reply

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...