0

I've extended Window to add some functionality, and part of this is the ability to specify a specific window size or allow it to size to the content. The codebehind looks like this, currently un-MVVMified.

public partial class DialogWindow : Window
{
    public bool HasSize { get; set; }

    public Size Size { get; set; }
}

The XAML then looks like this:

<Window ... Name="DialogWindowElement">
    <Window.Style>
        <Style TargetType="Window">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasSize, ElementName=DialogWindowElement}" Value="True">
                    <Setter Property="Width" Value="{Binding Size.Width, ElementName=DialogWindowElement}" />
                    <Setter Property="Height" Value="{Binding Size.Height, ElementName=DialogWindowElement}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding HasSize, ElementName=DialogWindowElement}" Value="False">
                    <Setter Property="SizeToContent" Value="WidthAndHeight" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Style>
    <ContentControl ...>
        <!-- Content control using DataTemplates to determine content -->
    </ContentControl>
</Window>

Resizing to content seems to work okay, but the specified width and height aren't applied. Any large content expands to all the size it needs instead of being constrained and then resizable later.

Snoop and other such tools imply the trigger is fired, but the setters don't seem to be having any effect.

Am I missing something here?

Edit: Added content control to the window to provide some more context

Dan
  • 1,130
  • 2
  • 20
  • 38
  • The Width and Height Bindings should be TwoWay. See e.g. here: https://stackoverflow.com/q/2673600/1136211 – Clemens Dec 20 '17 at 15:04
  • @Clemens No joy. I set the Width and Height bindings to `TwoWay` above, and also then tried separating the bindings to two separate Width and Height double properties. Neither approach resulted in the window obeying size. – Dan Dec 20 '17 at 15:27
  • Your sample code is a bit confusing.Are you actually setting the HasSize and Size properties? Are you setting the Height and Width properties of the window in the XAML? – mm8 Dec 20 '17 at 15:46

1 Answers1

0

This works for me:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    public bool HasSize { get; set; } = true;

    public Size Size { get; set; } = new Size(800, 800);
}

XAML:

<Window x:Class="WpfApplication1.Window21"
        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"
        mc:Ignorable="d"
        Title="Window1"
        Name="DialogWindowElement">
    <Window.Style>
        <Style TargetType="Window">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasSize, ElementName=DialogWindowElement}" Value="True">
                    <Setter Property="Width" Value="{Binding Size.Width, ElementName=DialogWindowElement}" />
                    <Setter Property="Height" Value="{Binding Size.Height, ElementName=DialogWindowElement}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding HasSize, ElementName=DialogWindowElement}" Value="False">
                    <Setter Property="SizeToContent" Value="WidthAndHeight" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Style>

    <TextBlock Text="Test..." FontSize="40" FontWeight="Bold" />
</Window>

Make sure that you don't set the Width and Height properties of the window in your XAML because local values take take precedence over values set by style setters.

mm8
  • 163,881
  • 10
  • 57
  • 88