Rabbi
Rabbi

Reputation: 4722

wpf Window Width not binding

I would like to control the height and width of one of my windows through my ViewModel.

This seems simple enough.

<Window ... Width="{Binding Path=Width}" Height="{Binding Path=Height}" />

but nope. it doesn't work.

It checks the ViewModel's Width but not the Height.

Strangely enough, if I switch the order of Width and Height in the XAML it checks the Height and not the Width. i.e. It only checks the first of the two properties and totally ignores the second one.

Binding the MaxHeight and MinHeight do work, even after the Width. But then the user can not re-size the window.

Upvotes: 2

Views: 5107

Answers (6)

M463
M463

Reputation: 2183

I assume that you're using MVVM pattern with the common methods for raising any changed propertys from the ViewModel to the View, by an implementation of the INotifiyPropertyChanged-Interface in your ViewModel.

Not working:

WdwWidth = 600;
WdwHeight = 600;

RaisePropertyChanged("WdwWidth");
RaisePropertyChanged("WdwHeight");

Working:

WdwWidth = 600;
RaisePropertyChanged("WdwWidth");

WdwHeight = 600;
RaisePropertyChanged("WdwHeight");

It seems to me that the PropertysChanged-notification has to be raised right after the property has been actually changed. Strange, but does the trick for me.

Edit: Make sure to set the Binding to TwoWay, e.g.Height="{Binding Path=WdwHeight, Mode=TwoWay}"

Upvotes: 0

Nat
Nat

Reputation: 1096

You want to bind to ActualHeight and ActualWidth, not Height and Width.

So instead of:

<Window ... Width="{Binding Path=Width}" Height="{Binding Path=Height}" />

You want:

<Window ... Width="{Binding Path=ActualWidth}" Height="{Binding Path=ActualHeight}" />

Upvotes: 0

h.alex
h.alex

Reputation: 902

you should have something like this in your window's load event:

    public void AfterLoad()
    {
        this.Height = this.ViewModel.Settings.WindowHeight;
        this.Width = this.ViewModel.Settings.WindowWidth;

        if (this.ViewModel.Settings.WindowTop > 0 && this.ViewModel.Settings.WindowLeft > 0)
        {
            this.Top = this.ViewModel.Settings.WindowTop;
            this.Left = this.ViewModel.Settings.WindowLeft;
        }
    }

then, handle the window's size changed event to remember the widht and height and also the position changed to remember top,left (if you wish).

binding to WindowState works fine.

Upvotes: 1

eFloh
eFloh

Reputation: 2158

This is correct by design (sad but true). The layout system does not actually enforce these values, see the remarks on the msdn documentation page for Window.Height... In some cases, it is acceptable to set MinWidth and MaxWidth (resp. *Height) to achive a similar behaviour, but not always.

Sorry to have no better news...

Upvotes: 0

Ruleland
Ruleland

Reputation: 21

Don't know what your ViewModel code looks like, but try creating a set for the Width and Height properties and set the binding Mode to TwoWay

Upvotes: 2

Shawn
Shawn

Reputation: 1030

I'm having this exact problem also, it seems like a bug.

For now I am just handling the DataContextChanged event of the Window and setting the Width and Height from the ViewModel manually.

Upvotes: 1

Related Questions