locked
How to bind a GridViewColumn's width to a property? RRS feed

  • Question

  • In an MVC WPF project I have a ListVIew that uses a GridView to display items. In order to save column widths I thought that I'd just bind each GridViewColumn's Width member to a property in the Model. This works in that the value in the property is being asserted to the GridViewColumn but the reverse is not try. Manually resizing the column's width does not result in a publication of a change in the column's width to the Model.

    Here's is a (simplified) example of the XAML code.

    <ListView
        DataContext="{Binding Source={StaticResource LogViewerWindowModel}}" >
        <ListView.View>
            <GridView>
    
                <GridViewColumn 
                    Width="{Binding LogCol1Width}" 
                    />
    
            </GridView>
        </ListView.View>
    </ListView>
    

    And there's nothing special about the Model properties. Standard sort of thing.

    public double LogCol1Width
    {
        get { return _LogCol1Width; }
        set
        {
            if (_LogCol1Width != value)
            {
                _LogCol1Width = value;
                OnPropertyChanged( "LogCol1Width" );
            }
        }
    }
    private double _LogCol1Width = 160;
    

    When the View first loads it requests the Property's value. However, no changes made by the user are broadcast to the property.

    What am I doing wrong and how can I get the control to notify the Model when a column's width changes?


    Richard Lewis Haggard

    Monday, November 9, 2015 1:46 AM

Answers

  • >>What am I doing wrong and how can I get the control to notify the Model when a column's width changes?

    You should set the Mode of the binding to TwoWay:

    <GridViewColumn Width="{Binding LogCol1Width, Mode=TwoWay}" />
    Then the setter of your source property should get set when you resize the column using the mouse.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, November 9, 2015 4:26 PM