2

Binding column width like this

<ListView ...>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="{Binding Width, Mode=TwoWay}" ... />
            ... more columns
        </GridView>
    </ListView.View>
</ListView>

The problem: when double clicking column header line (to autosize column) no Width setter is triggered, which means binding source is not updated after such column width changing. Normal column resizing works without problems.

Suggestions?

I don't want to prevent double-click autosizing, only to fix an issue.

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • What is `Width` in this context? Make sure your DataContext is set, check the output window for clues. – Mike Eason Jun 10 '15 at 12:35
  • @MikeEason, everything is working (normal column resizing in both directions - by code or by dragging column header line) until you double-click. – Sinatr Jun 10 '15 at 12:45
  • Ok, does the width actually change? I mean, you can double click, but if the width of the column doesn't change, then the bound property will not be updated. Try putting a breakpoint in the **set** part of your property (assuming it's just a CLR property) to see if it hits. – Mike Eason Jun 10 '15 at 12:48
  • It does changes. I noticed issue by what the width set by double click is not memorized and if I restart application - previous width is set. And breakpoint in setter was the first thing I did. It's not triggered when you double-click and width visually changed. – Sinatr Jun 10 '15 at 12:57

3 Answers3

1

Looking at the double click handler source code, it looks like the Width property should be updated. Perhaps something is going wrong in the binding? You might want to make your binding verbose and see what prints in the output window.

That issue aside, you may not get the behavior you desire by binding to the Width property. As you can tell from the double click handler, it gets set to NaN to make the column automatically resize. This means that even if your property setter gets called, it is going to get NaN passed to it. You could bind to ActualWidth using a OneWayToSource binding mode, unless you actually need the binding to be TwoWay for other reasons.

Xavier
  • 3,254
  • 15
  • 22
  • Interesting observations, thanks. Unfortunately I need `TwoWay` binding to restore column width from code (when application is started). – Sinatr Jun 10 '15 at 13:01
  • *Verbose binding* is really great, thanks. Your `NaN` observations helped, there is indeed *System.Windows.Data Error: 7 : ConvertBack cannot convert value 'n. def.' (type 'Double')* in Output window, which cause problems with binding.. to `int`, lol. – Sinatr Jun 10 '15 at 13:40
1

In my case I've solved the question doing this:

1. I have a property "_gridView" which is the GridView object representing the view of a ListView whose name is "_listView".

<ListView x:Name="_listView">
    <ListView.View>
        <GridView x:Name="_gridView">
            <GridViewColumn Header="Whatever"/>
        </GridView >
    </ListView.View>
</ListView>

2. I haved added this other piece of code to updated the width of the columns of the GridView. This is based on this .NET reference of GridViewColumnHeader.

private void UpdateWidths()
{
    foreach (var column in this._gridView.Columns)
    {
        column.Width = column.ActualWidth;
        column.Width = Double.NaN;
    }
}

3. And in the constructor of the WPF window, I've inserted this other code to execute the update of the widths each time the "ItemsSource" of "_listView" changes.:

public MainWindow()
{
    InitializeComponent();

    DependencyPropertyDescriptor
        .FromProperty(ListView.ItemsSourceProperty, typeof(ListView))
        .AddValueChanged(_listView, (s, e) => {
            UpdateWidths();
    });
}

4. Therefore, each time I change the "ItemsSource" property of the object "_listView" all the columns are updated.

_listView.ItemsSource = __New value__
Axel O'Connell
  • 626
  • 6
  • 4
0

I'd prefer to delete my question, but perhaps it will be usefull (I doubt) to someone else doing same mistake.

Problem was with Width type. I used int and it seems to works flawlessly. But it doesn't work with double-click resizing for whatever reasons.

To fix: use double Width {get; set;} to bind to, not int.

Sinatr
  • 20,892
  • 15
  • 90
  • 319