Frage

below is my code ,

 public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private int myHeight = 0;

        public int MyHeight
        {
            get { return myHeight; }
            set
            {
                myHeight = value;
                OnPropertyChanged("MyHeight");
                MessageBox.Show("MainWindowHeight" + MyHeight);
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        private void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

<Window x:Class="Window_Sample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="{Binding MyHeight, Mode=TwoWay}" Width="525">

    <Grid>

    </Grid>
</Window>

When i change my windowstate from minimized to maximized the setter get called twice,for the first time it have correct actual value,and in second call it has value 38,is there any explanation for this behavior ? and also why it is getting called twice?

Thanks, Kumar

War es hilfreich?

Lösung

If you look at the call stack for the two times the property is accessed you will see the second one goes through a function called UpdateDimensionsToRestoreBounds. Doing a web search on this led me here

http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Window.cs#c8ff58cc4568752a

The comment on this function says that when the window is maximised or minimised the intent is to have the property hold the value when not maximised or minimised. It looks like this is the reason for the two calls, the second is to set the values they will be when not maximised or minimised.

If you want to know the height of the window you can use

double renderedHeight = Application.Current.MainWindow.ActualHeight;

Hope this helps

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top