2

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.

Tim Penner
  • 3,551
  • 21
  • 36
Rabbi
  • 4,622
  • 9
  • 35
  • 45

6 Answers6

2

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

Ruleland
  • 21
  • 2
1

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.

Shawn
  • 970
  • 11
  • 9
  • Thanks - This comment seems like the only idea that works for me, but I still think we should be able to change a bound propertly and see the result reflected in the UI.. Anybody actually get this to work properly? – Chris Oct 16 '14 at 22:23
1

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.

h.alex
  • 902
  • 1
  • 8
  • 31
0

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}" />
Nat
  • 1,085
  • 2
  • 18
  • 35
0

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

M463
  • 2,003
  • 3
  • 23
  • 39
0

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...

eFloh
  • 2,098
  • 20
  • 24
  • oh, and also see [this other thread](http://stackoverflow.com/questions/4806088/wpf-finding-position-of-maximized-window/6525577#6525577) – eFloh Jun 29 '11 at 18:42