3
votes

I am trying to bind ChildWindow Height property to my viewmodel property , but I think it only reads VM value on first load and doesn't change size when VM changes & notifies about the change. In debugger I see that it goes into Height getter once, further notifications don't change ChildWindow size..

I think it should be bindable so I am wondering whether some issue exists here or I am doing some mistake?

Sounds like one time binding, but its oneway..

Height="{Binding WindowHeight,Mode=OneWay}" 

Further investigation shows that when we change binding to Mode=TwoWay and add an empty setter it begins to behave as expected. But that doesn't explain the reason why OneWay binding doesn't work. Also value that is passed to setter is equal to my whole application height, not just childwindow that is obviously supposed to be smaller.

The most strange thing with this whole situation is the following:

Also this value is passed to setter 4 consecutive times everytime after a getter is called (see count++ that is used to count that).

It is fired before dialog is actually shown, and it always goes in sequence get,set,set,set,set

Code for view model is super simple. Nowhere in code anyone is using ChildWindow Height, its only set in its xaml binding as shown above.

    private int count = 0;
    public int WindowHeight
    {
        get { return IsDefaultMode? DEFAULT_HEIGHT : SPECIAL_HEIGHT; }
        set {count++; }
    }

My inheriting Childwindow class contains like 5 strings of text none of which affect Height in any way.

Notification about WindowHeight is not fired by WindowHeight property (as seen in code) , its fired by Mode property. Couldve been a converter around mode but its currently implemented this way cause I am not sure a special converter with a couple of magic values for this situation is a better approach.

oks . mode setter code:

public bool IsSpecialMode
        {
            get { return m_IsSpecialMode; }
            set
            {
                if (m_IsSpecialMode!= value) 
                {
                    m_IsSpecialMode= value;
                    NotifyPropertyChanged("IsSpecialMode");
                    NotifyPropertyChanged("WindowHeight");
                }
            }
        }
1
You should check your "Output" window in Visual Studio for more information if the binding is breakingRumplin
@HiTech Magic: +1 for stumping me at the moment. Deleted my answer as not helpful enough. Will repost if I find something more useful for you.Gone Coding

1 Answers

1
votes

If the ChildWindow, or any other object, changes the Height property then your binding will be lost. Try setting it to a TwoWay binding and set a break point in your View-model's WindowHeight property's setter. That will tell you what is setting it and whether you can have a OneWay binding.

The ChildWindow class will actually set it's own Height and Width properties. For example, the following code ensures the ChildWindow always covers the root content of your application. This allows the ChildWindow to provide the overlay or faded effect when showing it's popup:

private void UpdateOverlaySize()
{
    if (((this.Overlay != null) && (Application.Current != null)) && ((Application.Current.Host != null) && (Application.Current.Host.Content != null)))
    {
        base.Height = Application.Current.Host.Content.ActualHeight;
        base.Width = Application.Current.Host.Content.ActualWidth;
        // ... other code removed
    }
}

So if effect, it looks like you can't use a OneWay binding on the Height or Width properties.