0

Update: This issue might be related to Problems with binding to Window Height and Width

I'm trying to change the window height through the view model property. For some reason, it does not work.

This is the window class:

public partial class MyWindow
{
    public MyWindow()
    {
        InitializeComponent();

        DataContext = new MyWindowViewModel();

        // Does not work!
        // ((MainWindowViewModel) DataContext).Height = 50;

        // Works:
        //Height = 50;

MyWindow.xaml

<ctrls:MyBaseWindow x:Class="....MyWindow"
              ...
              Width="{Binding Width}"
              Height="{Binding Height}"
              d:DataContext="{d:DesignInstance viewModels:MyWindowViewModel}">

MyWindowViewModel.cs

public class MainWindowViewModel : ReactiveObject
{
    ...

    public int Height
    {
        get { return _height; }
        set { this.RaiseAndSetIfChanged(ref _height, value); }
    }
Community
  • 1
  • 1
Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85

1 Answers1

1

I checked it inside my Visual Studio and it didn't worked when property SizeToContent of my window was set to "Height". When I removed it everything worked fine.

You should as well changed the order of the lines:

InitializeComponent();
DataContext = new MyWindowViewModel();

to:

DataContext = new MyWindowViewModel();
InitializeComponent();
mr100
  • 4,340
  • 2
  • 26
  • 38
  • You should change the order of instructions in constructor of your window class - this should be the solution. I've editted my post to show that. – mr100 May 02 '14 at 11:12
  • Actually, the changing of the order fixes original issue. But it looks like the binding is modified so I cannot change the window size by modifying the model properties. – Alex Netkachov May 02 '14 at 11:34
  • And does your model implement properly INotifyChanged? – mr100 May 02 '14 at 11:52
  • Yes, it is completely reactive (extends ReactiveObject). Actually, specifying two-way databinding for width and height solved the issue. Thank you for your help. – Alex Netkachov May 02 '14 at 12:28