Search code examples
c#wpfdialogprismsizing

WPF Prism Dialog Window - Binding to the DialogWindow's Width and Height


I am trying to save the width and height of a Prism WPF dialog to be retrieved on next use of the dialog.

I have a static class that is being serialized into xml to save the properties, so I tried to hook it up in the UserControl's code behind by injecting it in the constructor and link it to WindowWidth and WindowHeight properites and used binding to a named element in xaml like so:

User control xaml:

    <UserControl
     x:Class="MyNameSpace.Views.MyView"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:behaviors="clr-namespace:Microdesk.BIMrxCommon.WpfUi.Behaviors;assembly=Microdesk.BIMrxCommon.WpfUi"
     xmlns:core="clr-namespace:System;assembly=mscorlib"
     xmlns:data="clr-namespace:System.Data;assembly=System.Data"
     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
     xmlns:mvvm="http://prismlibrary.com/"
     x:Name="MyViewControl"
     mvvm:ViewModelLocator.AutoWireViewModel="True">
    
        <mvvm:Dialog.WindowStyle>
            <Style TargetType="Window">
                <Setter Property="mvvm:Dialog.WindowStartupLocation" Value="CenterScreen" />
                <Setter Property="ResizeMode" Value="CanResizeWithGrip" />
                <Setter Property="ShowInTaskbar" Value="False" />
                <Setter Property="SizeToContent" Value="Manual" />
                <Setter Property="Height" Value="{Binding WindowHeight, Mode=TwoWay, ElementName=MyViewControl, UpdateSourceTrigger=PropertyChanged}" />
                <Setter Property="Width" Value="{Binding WindowWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ElementName=MyViewControl}" />
    
            </Style>
    
        </mvvm:Dialog.WindowStyle>
....

Code behind:

    public partial class MyView : UserControl    {

        public IApplicationSettings ApplicationSettings { get; set; }\\Injected in ctor

        public double WindowHeight
        {
            get => ApplicationSettings.MyViewHeight;
            set => ApplicationSettings.MyViewHeight = value;
        }

        public double WindowWidth        {
            get => ApplicationSettings.MyViewWidth;
            set => ApplicationSettings.MyViewWidth = value;
        }
...

But the binding is not finding the named UserControl, instead, it is trying to find the properties in DialogWindow object:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MyViewControl'. BindingExpression:Path=WindowHeight; DataItem=null; target element is 'DialogWindow' (Name=''); target property is 'Height' (type 'Double')

not sure if this is the right approach in the first place, any help is thankfully appreciated.

Update:

I tried adding the WindowWidth and WindowHeight properties to the ViewModel instead of code behind. The output window is not showing any complains regarding the binding so it should be fine, but still, changing window size on runtime will not set those properties in the view model, it is if like the actual window width and height are defined separately from the ones I'm binding to in xaml.


Solution

  • It turned out that binding to properties in the view model instead of code behind as described in the question update works. The reason it was not working when I first tried it is that I have a custom dialog window registered and that window had the height and width hard coded in xaml so it was not responding to the bindings. I just removed the hard coded values and the binding now works.