1

When I maximize the window of a wpf application the bound width updates twice. First in the correct width and second with the previous width.

This is the code I use. (I tried putting it in a seperate project)

XAML

<catel:Window x:Class="WPFTestApplication1.Views.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:catel="http://schemas.catelproject.com"
          ResizeMode="CanResize" Width="{Binding W,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="{Binding H,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
          MinHeight="400" MinWidth="600">

     <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="{Binding Title}" />
        <Label Grid.Row="1" Grid.Column="0" Content="Here goes your real content" />
    </Grid>

</catel:Window>

MainWindowViewModel:

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
    }

    public override string Title { get { return "WPFTestApplication1"; } }

    // TODO: Register models with the vmpropmodel codesnippet
    // TODO: Register view model properties with the vmprop or vmpropviewmodeltomodel codesnippets
    // TODO: Register commands with the vmcommand or vmcommandwithcanexecute codesnippets

    protected override async Task InitializeAsync()
    {
        await base.InitializeAsync();

        // TODO: subscribe to events here
    }

    protected override async Task CloseAsync()
    {
        // TODO: unsubscribe from events here

        await base.CloseAsync();
    }


    public double H
    {
        get { return GetValue<double>(HProperty); }
        set { SetValue(HProperty, value); Console.WriteLine(value); }
    }

    public static readonly PropertyData HProperty = RegisterProperty(nameof(H), typeof(double), null);


    public double W
    {
        get { return GetValue<double>(WProperty); }
        set { SetValue(WProperty, value); Console.WriteLine(value); }
    }

    public static readonly PropertyData WProperty = RegisterProperty(nameof(W), typeof(double), null);
}

I need the values to scale something to the window and it ends with the wrong value. How do I fix this?

  • What if you simply attach a SizeChanged handler that updates the view model? – Clemens May 16 '18 at 14:03
  • Tip: if you want to remember (store / restore) size of a window, use `Orchestra.Core` and use the `SaveWindowSave` and `LoadWindowSize` extension methods. – Geert van Horrik May 16 '18 at 14:11
  • 1
    Hi, first: Width is to define what you want (desired) - to get the real width use ActualWidth second : why did you create dependency property (H) in your view model ? and last : why you care about your window size in your viewmodel, that's not business... bye – GCamel May 16 '18 at 14:12
  • There's a size change handler that gets the correct hieght values when the window is maximized here: https://stackoverflow.com/questions/32668707/c-sharp-wpf-sizechanged-event-doesnt-update-width-and-height-when-maximized – L0uis May 16 '18 at 14:46
  • @GCamel 1 How would I get the actual width? 2 I need the height and width. orry I didn't specify. 3 I want to make a table with the amount of rows dependant on the height – K.Zuiderduin May 17 '18 at 09:51
  • Well, you can bind ActualWidth to a property in your viewmodel but you will get a lot of changes when resizing...Changing the number of rows depending the screen size is very Strange...why not using scrolling ? and / or paging on your table ? – GCamel May 17 '18 at 09:58

0 Answers0