防止 wpf listview 标题双击自动调整列 [英] prevent wpf listview header double click autosizes column

查看:33
本文介绍了防止 wpf listview 标题双击自动调整列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,其中我对列标题进行了模板化,并且列表视图项也被模板化了.但是,对于网格视图中的某些行,我有不同的模板.当用户双击可以拖动列宽度的列表视图列标题时,列标题将自动调整大小,这意味着它会增加其大小.这给我带来了问题,因为我的列标题宽度不再与行模板中的列宽度同步.

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?

推荐答案

是的,在 ListView 本身上设置一个双击处理程序.然后在处理程序中,使用如下代码:

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

其中 TryFindParent 定义为:

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

这篇关于防止 wpf listview 标题双击自动调整列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆