0

I have a simple MVVM pattern, where I have a ViewModel and View (the only 2 problems in this case):

ViewModel:

public class LoginWindowViewModel : Bases.ViewModelBase
{
    private double _width = 400;
    public double Width
    {
        get { return _width; }
        set { SetProperty(ref _width, value); }
    }

    private double _height = 500;
    public double Height
    {
        get { return _height; }
        set { SetProperty(ref _height, value); }
    }

    private BitmapImage _backgroundImage = null;
    public BitmapImage BackgroundImage
    {
        get
        {
            if (_backgroundImage == null) _backgroundImage = Model.Imagining.GetRandomImage((int)Width, (int)Height);
            return _backgroundImage;
        }
    }

}

View:

public partial class LoginWindow : Window
{
    public LoginWindowViewModel ViewModel
    {
        get { return (LoginWindowViewModel)DataContext; }
    }

    public LoginWindow()
    {
        InitializeComponent();
    }
    private void mainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Open();
    }



    public void Open()
    {
        Open(Settings.Default.AnimationDuration);
    }
    public void Open(Duration animDuration)
    {
        UpdateLayout();
        mainWindow.Clip = new EllipseGeometry(new Point(mainWindow.Width / 2, mainWindow.Height / 2), 0, 0);
        double radius = MathFunctions.CalculatePitagoras(mainWindow.Width, mainWindow.Height) / 2;

        Animations.RadiusXAnimation((EllipseGeometry)mainWindow.Clip, radius, animDuration);
        Animations.RadiusYAnimation((EllipseGeometry)mainWindow.Clip, radius, animDuration);
    }

}

View XAML:

<Window x:Name="mainWindow" x:Class="Gestionale.View.LoginWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Gestionale.View"
    xmlns:viewModels="clr-namespace:Gestionale.ViewModel"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    mc:Ignorable="d"
    Style="{DynamicResource MaterialDesignCardWindow}"
    Background="{x:Null}"
    Title="LoginWindow" WindowStartupLocation="CenterScreen" ResizeMode="NoResize"
    Width="{Binding Width, Mode=TwoWay}"
    Height="{Binding Height, Mode=TwoWay}"
    Loaded="mainWindow_Loaded">
    <Window.DataContext>
        <viewModels:LoginWindowViewModel />
    </Window.DataContext>
    <Grid x:Name="mainGrid">
        <Image Source="{Binding BackgroundImage}" Margin="-1" />
        <Rectangle Fill="{DynamicResource PrimaryHueMidBrush}" />
        <Grid>
        ...
        </Grid>
    </Grid>
</Window>

So as you can see, I made a simple binding between ViewModel and View, where Height and Width are set up in ViewModel, where the reason is that when I get an Image which is downloaded from the internet, it's automatically bound to the View's image. I want to do an opening animation as soon as the Window is Loaded called like this:

public MainWindow()
    {
        if(LoggedUser == null)
        {
            LoginWindow newLoginWindow = new LoginWindow();
            newLoginWindow.ShowDialog();
        }
        InitializeComponent();
    }

But my exact problem is that when I call the Loaded event, one of the values (either Width or Height, it depends of the position it is bound in the XAML) is different, and it's changed AFTER the Loaded callout, what I checked by making a breakpoint on SizeChanged event (which is not here). Basically (in this case) it's going like this: Initialized (width: random, height: random) -> Width: 400 Height: Random (749 in my case everytime for some reason) -> Loaded -> Width: 400 Height: 500

If I invert the binding position in XAML to Height first and Width second it's the same situation but Height is correct and Width is being changed after Loaded. It is a quite problem for me since I try to make a correct animation but the center point for me is different.

Kuba Wasilczyk
  • 1,118
  • 3
  • 11
  • 20
  • First of all code behind of your view is not the model. It's just a part of the view. Also why do you bind two way? (at `Mode=TwoWay`) – Emad Mar 13 '18 at 11:38
  • I bound two way in case I will change size in the future by code behind. Tried OneWay or without specifying Mode (which should put it back on OneWay if I'm not wrong) but it doesn't change anything. And I wrote "Model" by mistake. Gonna correct it now. – Kuba Wasilczyk Mar 13 '18 at 11:41
  • Correcting myself. Changing to OneWay, makes so the second size value doesn't change at all, remains the "random" one. Following the example above, Width changes to 400 and Height stays 749 even after Loaded, and animation. – Kuba Wasilczyk Mar 13 '18 at 11:45

0 Answers0