开发者

prevent wpf listview header double click autosizes column

开发者 https://www.devze.com 2022-12-28 05:01 出处:网络
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 click

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.

I开发者_JAVA百科s there a quick and easy way to prevent this double click behavior on the header of a column?


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


I found working solution after digging in GridViewColumnHeader source code. My XAML for ListView with columns is:

                            <ListView.View>
                                <GridView AllowsColumnReorder="False" x:Name="ListGridView">
                                    <GridView.Columns>

                                        <GridViewColumn x:Name="ExpandHeader"
                                                        Width="40">
                                            <GridViewColumn.Header>
                                                <GridViewColumnHeader IsHitTestVisible="False" />
                                            </GridViewColumn.Header>
                                        </GridViewColumn>

And you need to put code like this in your View's Loaded event (when columns are created):

    private void ViewOnLoaded(object sender, RoutedEventArgs e)
    {
        var fields = typeof(GridViewColumnHeader).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
        var thumbFieldInfo = fields.FirstOrDefault(fi => fi.FieldType == typeof(Thumb));
        var methods = typeof(GridViewColumnHeader).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
        var eventHandlerMethodInfo = methods.FirstOrDefault(mi => mi.Name == "OnGripperDoubleClicked");

        if (thumbFieldInfo != null && eventHandlerMethodInfo != null)
        {
            foreach (var column in ListGridView.Columns)
            {
                var header = column.Header as GridViewColumnHeader;
                if (header != null)
                {
                    var headerGripper = thumbFieldInfo.GetValue(header) as Thumb;
                    if (headerGripper != null)
                    {
                        var handler = Delegate.CreateDelegate(typeof(MouseButtonEventHandler), header, eventHandlerMethodInfo);
                        headerGripper.RemoveHandler(Control.MouseDoubleClickEvent, handler);
                    }
                }
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消