SizeToContent issue with custom windowstyle when start












0















My problem is: I have two sizes of window which changes according to CheckBox. Below have a screenshot of the problem:



enter image description here



Like you can see, my app starts with incorrect size but when I change the checkbox everything works fine, the window size become normal



enter image description hereenter image description here



I already tested to set CheckBox when app starts by code behind and not work.



Here's my XAML of custom windowstyle



    <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
<Setter Property="shell:WindowChrome.WindowChrome">
<Setter.Value>
<shell:WindowChrome
GlassFrameThickness="-1"
ResizeBorderThickness="5"
CaptionHeight="36"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FF2D2D30" />
<Setter Property="Background" Value="#FF2D2D30" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="BorderThickness" Value="15,35,15,15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border
x:Name="MainBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>
</Border>

<DockPanel
x:Name="MainDock"
VerticalAlignment="Top"
LastChildFill="False">

<TextBlock
VerticalAlignment="Bottom"
DockPanel.Dock="Left"
Margin="20 0 0 0"
FontSize="14"
Foreground="#ff999999"
Text="{TemplateBinding Title}" />

<Button x:Name="btnClose"
Width="15"
Margin="5"
Click="CloseClick"
Content="X"
DockPanel.Dock="Right"
Background="Transparent"
BorderBrush="Transparent"
Foreground="White"
VerticalContentAlignment="Center"
WindowChrome.IsHitTestVisibleInChrome="True"
/>

<Button
x:Name="btnRestore"
Width="15"
Margin="5"
Click="MaximizeRestoreClick"
Content="#"
DockPanel.Dock="Right"
Background="Transparent"
BorderBrush="Transparent"
Foreground="White"
VerticalContentAlignment="Center"
WindowChrome.IsHitTestVisibleInChrome="True"/>

<Button
x:Name="btnMinimize"
Width="15"
Margin="5"
Click="MinimizeClick"
Content="_"
DockPanel.Dock="Right"
Background="Transparent"
BorderBrush="Transparent"
Foreground="White"
VerticalContentAlignment="Center"
WindowChrome.IsHitTestVisibleInChrome="True" />
</DockPanel>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsFullScreen}" Value="True">
<Setter Property="Visibility" Value="Collapsed" TargetName="MainDock"/>
<Setter Property="BorderThickness" Value="0 0 0 0"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


XAML code of window



<Window x:Class="STEP_QuickSound_UI.Window_SelectConnection"
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="QuickSoundX"
Closing="Window_Closing"
Topmost="true"
Background="{StaticResource WindowBackground}"
Style="{StaticResource CustomWindowStyle}"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<RowDefinition/>
<RowDefinition Height="0.2*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Label Margin="5" Foreground="White">Tipo de conexão</Label>
<StackPanel Orientation="Horizontal">
<RadioButton Margin="5" Foreground="White" IsChecked="True" Name="RemoteConnection" GroupName="Connection">Remota</RadioButton>
<RadioButton Margin="5" Foreground="White" Name="LocalConnection" GroupName="Connection">Local</RadioButton>
</StackPanel>
</StackPanel>

<StackPanel Grid.Row="1" >
<Label Foreground="White" Margin="5">IP Address</Label>
<TextBox Name="IpAddress" Margin="5">192.168.0.5</TextBox>

<Label Foreground="White" Margin="5">Porta</Label>
<TextBox Name="Port" Margin="5">8281</TextBox>
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=RemoteConnection, Path=IsChecked}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
<StackPanel Grid.Row="2" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<Button Margin="5" Grid.Column="0" Style="{StaticResource Button}" Click="SaveClick">Salvar</Button>
<Button Margin="5" Grid.Column="1" Style="{StaticResource Button}" Click="CancelClick">Cancelar</Button>
</Grid>
</StackPanel>
</Grid>


ADDEND:



If I remove the custom window style, works fine, so it's something i'm missing there.










share|improve this question





























    0















    My problem is: I have two sizes of window which changes according to CheckBox. Below have a screenshot of the problem:



    enter image description here



    Like you can see, my app starts with incorrect size but when I change the checkbox everything works fine, the window size become normal



    enter image description hereenter image description here



    I already tested to set CheckBox when app starts by code behind and not work.



    Here's my XAML of custom windowstyle



        <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
    <Setter Property="shell:WindowChrome.WindowChrome">
    <Setter.Value>
    <shell:WindowChrome
    GlassFrameThickness="-1"
    ResizeBorderThickness="5"
    CaptionHeight="36"/>
    </Setter.Value>
    </Setter>
    <Setter Property="BorderBrush" Value="#FF2D2D30" />
    <Setter Property="Background" Value="#FF2D2D30" />
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="BorderThickness" Value="15,35,15,15"/>
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type Window}">
    <Grid>
    <Border
    x:Name="MainBorder"
    Background="{TemplateBinding Background}"
    BorderBrush="{TemplateBinding BorderBrush}"
    BorderThickness="{TemplateBinding BorderThickness}">
    <AdornerDecorator>
    <ContentPresenter />
    </AdornerDecorator>
    </Border>

    <DockPanel
    x:Name="MainDock"
    VerticalAlignment="Top"
    LastChildFill="False">

    <TextBlock
    VerticalAlignment="Bottom"
    DockPanel.Dock="Left"
    Margin="20 0 0 0"
    FontSize="14"
    Foreground="#ff999999"
    Text="{TemplateBinding Title}" />

    <Button x:Name="btnClose"
    Width="15"
    Margin="5"
    Click="CloseClick"
    Content="X"
    DockPanel.Dock="Right"
    Background="Transparent"
    BorderBrush="Transparent"
    Foreground="White"
    VerticalContentAlignment="Center"
    WindowChrome.IsHitTestVisibleInChrome="True"
    />

    <Button
    x:Name="btnRestore"
    Width="15"
    Margin="5"
    Click="MaximizeRestoreClick"
    Content="#"
    DockPanel.Dock="Right"
    Background="Transparent"
    BorderBrush="Transparent"
    Foreground="White"
    VerticalContentAlignment="Center"
    WindowChrome.IsHitTestVisibleInChrome="True"/>

    <Button
    x:Name="btnMinimize"
    Width="15"
    Margin="5"
    Click="MinimizeClick"
    Content="_"
    DockPanel.Dock="Right"
    Background="Transparent"
    BorderBrush="Transparent"
    Foreground="White"
    VerticalContentAlignment="Center"
    WindowChrome.IsHitTestVisibleInChrome="True" />
    </DockPanel>
    </Grid>
    <ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding IsFullScreen}" Value="True">
    <Setter Property="Visibility" Value="Collapsed" TargetName="MainDock"/>
    <Setter Property="BorderThickness" Value="0 0 0 0"/>
    </DataTrigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>


    XAML code of window



    <Window x:Class="STEP_QuickSound_UI.Window_SelectConnection"
    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="QuickSoundX"
    Closing="Window_Closing"
    Topmost="true"
    Background="{StaticResource WindowBackground}"
    Style="{StaticResource CustomWindowStyle}"
    SizeToContent="WidthAndHeight"
    WindowStartupLocation="CenterScreen">
    <Grid >
    <Grid.RowDefinitions>
    <RowDefinition Height="0.5*"/>
    <RowDefinition/>
    <RowDefinition Height="0.2*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
    <Label Margin="5" Foreground="White">Tipo de conexão</Label>
    <StackPanel Orientation="Horizontal">
    <RadioButton Margin="5" Foreground="White" IsChecked="True" Name="RemoteConnection" GroupName="Connection">Remota</RadioButton>
    <RadioButton Margin="5" Foreground="White" Name="LocalConnection" GroupName="Connection">Local</RadioButton>
    </StackPanel>
    </StackPanel>

    <StackPanel Grid.Row="1" >
    <Label Foreground="White" Margin="5">IP Address</Label>
    <TextBox Name="IpAddress" Margin="5">192.168.0.5</TextBox>

    <Label Foreground="White" Margin="5">Porta</Label>
    <TextBox Name="Port" Margin="5">8281</TextBox>
    <StackPanel.Style>
    <Style TargetType="StackPanel">
    <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
    <DataTrigger Binding="{Binding ElementName=RemoteConnection, Path=IsChecked}" Value="True">
    <Setter Property="Visibility" Value="Visible"/>
    </DataTrigger>
    </Style.Triggers>
    </Style>
    </StackPanel.Style>
    </StackPanel>
    <StackPanel Grid.Row="2" >
    <Grid>
    <Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Button Margin="5" Grid.Column="0" Style="{StaticResource Button}" Click="SaveClick">Salvar</Button>
    <Button Margin="5" Grid.Column="1" Style="{StaticResource Button}" Click="CancelClick">Cancelar</Button>
    </Grid>
    </StackPanel>
    </Grid>


    ADDEND:



    If I remove the custom window style, works fine, so it's something i'm missing there.










    share|improve this question



























      0












      0








      0








      My problem is: I have two sizes of window which changes according to CheckBox. Below have a screenshot of the problem:



      enter image description here



      Like you can see, my app starts with incorrect size but when I change the checkbox everything works fine, the window size become normal



      enter image description hereenter image description here



      I already tested to set CheckBox when app starts by code behind and not work.



      Here's my XAML of custom windowstyle



          <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
      <Setter Property="shell:WindowChrome.WindowChrome">
      <Setter.Value>
      <shell:WindowChrome
      GlassFrameThickness="-1"
      ResizeBorderThickness="5"
      CaptionHeight="36"/>
      </Setter.Value>
      </Setter>
      <Setter Property="BorderBrush" Value="#FF2D2D30" />
      <Setter Property="Background" Value="#FF2D2D30" />
      <Setter Property="SnapsToDevicePixels" Value="true" />
      <Setter Property="BorderThickness" Value="15,35,15,15"/>
      <Setter Property="Template">
      <Setter.Value>
      <ControlTemplate TargetType="{x:Type Window}">
      <Grid>
      <Border
      x:Name="MainBorder"
      Background="{TemplateBinding Background}"
      BorderBrush="{TemplateBinding BorderBrush}"
      BorderThickness="{TemplateBinding BorderThickness}">
      <AdornerDecorator>
      <ContentPresenter />
      </AdornerDecorator>
      </Border>

      <DockPanel
      x:Name="MainDock"
      VerticalAlignment="Top"
      LastChildFill="False">

      <TextBlock
      VerticalAlignment="Bottom"
      DockPanel.Dock="Left"
      Margin="20 0 0 0"
      FontSize="14"
      Foreground="#ff999999"
      Text="{TemplateBinding Title}" />

      <Button x:Name="btnClose"
      Width="15"
      Margin="5"
      Click="CloseClick"
      Content="X"
      DockPanel.Dock="Right"
      Background="Transparent"
      BorderBrush="Transparent"
      Foreground="White"
      VerticalContentAlignment="Center"
      WindowChrome.IsHitTestVisibleInChrome="True"
      />

      <Button
      x:Name="btnRestore"
      Width="15"
      Margin="5"
      Click="MaximizeRestoreClick"
      Content="#"
      DockPanel.Dock="Right"
      Background="Transparent"
      BorderBrush="Transparent"
      Foreground="White"
      VerticalContentAlignment="Center"
      WindowChrome.IsHitTestVisibleInChrome="True"/>

      <Button
      x:Name="btnMinimize"
      Width="15"
      Margin="5"
      Click="MinimizeClick"
      Content="_"
      DockPanel.Dock="Right"
      Background="Transparent"
      BorderBrush="Transparent"
      Foreground="White"
      VerticalContentAlignment="Center"
      WindowChrome.IsHitTestVisibleInChrome="True" />
      </DockPanel>
      </Grid>
      <ControlTemplate.Triggers>
      <DataTrigger Binding="{Binding IsFullScreen}" Value="True">
      <Setter Property="Visibility" Value="Collapsed" TargetName="MainDock"/>
      <Setter Property="BorderThickness" Value="0 0 0 0"/>
      </DataTrigger>
      </ControlTemplate.Triggers>
      </ControlTemplate>
      </Setter.Value>
      </Setter>
      </Style>


      XAML code of window



      <Window x:Class="STEP_QuickSound_UI.Window_SelectConnection"
      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="QuickSoundX"
      Closing="Window_Closing"
      Topmost="true"
      Background="{StaticResource WindowBackground}"
      Style="{StaticResource CustomWindowStyle}"
      SizeToContent="WidthAndHeight"
      WindowStartupLocation="CenterScreen">
      <Grid >
      <Grid.RowDefinitions>
      <RowDefinition Height="0.5*"/>
      <RowDefinition/>
      <RowDefinition Height="0.2*"/>
      </Grid.RowDefinitions>
      <StackPanel Grid.Row="0">
      <Label Margin="5" Foreground="White">Tipo de conexão</Label>
      <StackPanel Orientation="Horizontal">
      <RadioButton Margin="5" Foreground="White" IsChecked="True" Name="RemoteConnection" GroupName="Connection">Remota</RadioButton>
      <RadioButton Margin="5" Foreground="White" Name="LocalConnection" GroupName="Connection">Local</RadioButton>
      </StackPanel>
      </StackPanel>

      <StackPanel Grid.Row="1" >
      <Label Foreground="White" Margin="5">IP Address</Label>
      <TextBox Name="IpAddress" Margin="5">192.168.0.5</TextBox>

      <Label Foreground="White" Margin="5">Porta</Label>
      <TextBox Name="Port" Margin="5">8281</TextBox>
      <StackPanel.Style>
      <Style TargetType="StackPanel">
      <Setter Property="Visibility" Value="Collapsed"/>
      <Style.Triggers>
      <DataTrigger Binding="{Binding ElementName=RemoteConnection, Path=IsChecked}" Value="True">
      <Setter Property="Visibility" Value="Visible"/>
      </DataTrigger>
      </Style.Triggers>
      </Style>
      </StackPanel.Style>
      </StackPanel>
      <StackPanel Grid.Row="2" >
      <Grid>
      <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
      </Grid.ColumnDefinitions>

      <Button Margin="5" Grid.Column="0" Style="{StaticResource Button}" Click="SaveClick">Salvar</Button>
      <Button Margin="5" Grid.Column="1" Style="{StaticResource Button}" Click="CancelClick">Cancelar</Button>
      </Grid>
      </StackPanel>
      </Grid>


      ADDEND:



      If I remove the custom window style, works fine, so it's something i'm missing there.










      share|improve this question
















      My problem is: I have two sizes of window which changes according to CheckBox. Below have a screenshot of the problem:



      enter image description here



      Like you can see, my app starts with incorrect size but when I change the checkbox everything works fine, the window size become normal



      enter image description hereenter image description here



      I already tested to set CheckBox when app starts by code behind and not work.



      Here's my XAML of custom windowstyle



          <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
      <Setter Property="shell:WindowChrome.WindowChrome">
      <Setter.Value>
      <shell:WindowChrome
      GlassFrameThickness="-1"
      ResizeBorderThickness="5"
      CaptionHeight="36"/>
      </Setter.Value>
      </Setter>
      <Setter Property="BorderBrush" Value="#FF2D2D30" />
      <Setter Property="Background" Value="#FF2D2D30" />
      <Setter Property="SnapsToDevicePixels" Value="true" />
      <Setter Property="BorderThickness" Value="15,35,15,15"/>
      <Setter Property="Template">
      <Setter.Value>
      <ControlTemplate TargetType="{x:Type Window}">
      <Grid>
      <Border
      x:Name="MainBorder"
      Background="{TemplateBinding Background}"
      BorderBrush="{TemplateBinding BorderBrush}"
      BorderThickness="{TemplateBinding BorderThickness}">
      <AdornerDecorator>
      <ContentPresenter />
      </AdornerDecorator>
      </Border>

      <DockPanel
      x:Name="MainDock"
      VerticalAlignment="Top"
      LastChildFill="False">

      <TextBlock
      VerticalAlignment="Bottom"
      DockPanel.Dock="Left"
      Margin="20 0 0 0"
      FontSize="14"
      Foreground="#ff999999"
      Text="{TemplateBinding Title}" />

      <Button x:Name="btnClose"
      Width="15"
      Margin="5"
      Click="CloseClick"
      Content="X"
      DockPanel.Dock="Right"
      Background="Transparent"
      BorderBrush="Transparent"
      Foreground="White"
      VerticalContentAlignment="Center"
      WindowChrome.IsHitTestVisibleInChrome="True"
      />

      <Button
      x:Name="btnRestore"
      Width="15"
      Margin="5"
      Click="MaximizeRestoreClick"
      Content="#"
      DockPanel.Dock="Right"
      Background="Transparent"
      BorderBrush="Transparent"
      Foreground="White"
      VerticalContentAlignment="Center"
      WindowChrome.IsHitTestVisibleInChrome="True"/>

      <Button
      x:Name="btnMinimize"
      Width="15"
      Margin="5"
      Click="MinimizeClick"
      Content="_"
      DockPanel.Dock="Right"
      Background="Transparent"
      BorderBrush="Transparent"
      Foreground="White"
      VerticalContentAlignment="Center"
      WindowChrome.IsHitTestVisibleInChrome="True" />
      </DockPanel>
      </Grid>
      <ControlTemplate.Triggers>
      <DataTrigger Binding="{Binding IsFullScreen}" Value="True">
      <Setter Property="Visibility" Value="Collapsed" TargetName="MainDock"/>
      <Setter Property="BorderThickness" Value="0 0 0 0"/>
      </DataTrigger>
      </ControlTemplate.Triggers>
      </ControlTemplate>
      </Setter.Value>
      </Setter>
      </Style>


      XAML code of window



      <Window x:Class="STEP_QuickSound_UI.Window_SelectConnection"
      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="QuickSoundX"
      Closing="Window_Closing"
      Topmost="true"
      Background="{StaticResource WindowBackground}"
      Style="{StaticResource CustomWindowStyle}"
      SizeToContent="WidthAndHeight"
      WindowStartupLocation="CenterScreen">
      <Grid >
      <Grid.RowDefinitions>
      <RowDefinition Height="0.5*"/>
      <RowDefinition/>
      <RowDefinition Height="0.2*"/>
      </Grid.RowDefinitions>
      <StackPanel Grid.Row="0">
      <Label Margin="5" Foreground="White">Tipo de conexão</Label>
      <StackPanel Orientation="Horizontal">
      <RadioButton Margin="5" Foreground="White" IsChecked="True" Name="RemoteConnection" GroupName="Connection">Remota</RadioButton>
      <RadioButton Margin="5" Foreground="White" Name="LocalConnection" GroupName="Connection">Local</RadioButton>
      </StackPanel>
      </StackPanel>

      <StackPanel Grid.Row="1" >
      <Label Foreground="White" Margin="5">IP Address</Label>
      <TextBox Name="IpAddress" Margin="5">192.168.0.5</TextBox>

      <Label Foreground="White" Margin="5">Porta</Label>
      <TextBox Name="Port" Margin="5">8281</TextBox>
      <StackPanel.Style>
      <Style TargetType="StackPanel">
      <Setter Property="Visibility" Value="Collapsed"/>
      <Style.Triggers>
      <DataTrigger Binding="{Binding ElementName=RemoteConnection, Path=IsChecked}" Value="True">
      <Setter Property="Visibility" Value="Visible"/>
      </DataTrigger>
      </Style.Triggers>
      </Style>
      </StackPanel.Style>
      </StackPanel>
      <StackPanel Grid.Row="2" >
      <Grid>
      <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
      </Grid.ColumnDefinitions>

      <Button Margin="5" Grid.Column="0" Style="{StaticResource Button}" Click="SaveClick">Salvar</Button>
      <Button Margin="5" Grid.Column="1" Style="{StaticResource Button}" Click="CancelClick">Cancelar</Button>
      </Grid>
      </StackPanel>
      </Grid>


      ADDEND:



      If I remove the custom window style, works fine, so it's something i'm missing there.







      c# wpf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 16:02









      mm8

      83.2k81931




      83.2k81931










      asked Nov 14 '18 at 11:19









      Kevin KouketsuKevin Kouketsu

      362213




      362213
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can get rid of this behaviour by removing SizeToContent="WidthAndHeight" from your XAML and setting the SizeToContent programmatically once the window has been loaded:



          public partial class Window_SelectConnection : Window
          {
          public Window_SelectConnection()
          {
          InitializeComponent();
          Loaded += OnLoaded;
          }

          private void OnLoaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= OnLoaded;
          }
          }


          You may want to create a custom window class that does this for you:



          public class CustomWindow : Window
          {
          public CustomWindow()
          {
          Loaded += CustomWindow_Loaded;
          }

          private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= CustomWindow_Loaded;
          }
          }


          ...and inherit from this one:



          public partial class Window_SelectConnection : CustomWindow

          <local:CustomWindow x:Class="STEP_QuickSound_UI.Window_SelectConnection" ...





          share|improve this answer
























          • I don't know why I did not tried Loaded event. Thanks, that code works.

            – Kevin Kouketsu
            Nov 14 '18 at 15:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299013%2fsizetocontent-issue-with-custom-windowstyle-when-start%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can get rid of this behaviour by removing SizeToContent="WidthAndHeight" from your XAML and setting the SizeToContent programmatically once the window has been loaded:



          public partial class Window_SelectConnection : Window
          {
          public Window_SelectConnection()
          {
          InitializeComponent();
          Loaded += OnLoaded;
          }

          private void OnLoaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= OnLoaded;
          }
          }


          You may want to create a custom window class that does this for you:



          public class CustomWindow : Window
          {
          public CustomWindow()
          {
          Loaded += CustomWindow_Loaded;
          }

          private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= CustomWindow_Loaded;
          }
          }


          ...and inherit from this one:



          public partial class Window_SelectConnection : CustomWindow

          <local:CustomWindow x:Class="STEP_QuickSound_UI.Window_SelectConnection" ...





          share|improve this answer
























          • I don't know why I did not tried Loaded event. Thanks, that code works.

            – Kevin Kouketsu
            Nov 14 '18 at 15:48
















          1














          You can get rid of this behaviour by removing SizeToContent="WidthAndHeight" from your XAML and setting the SizeToContent programmatically once the window has been loaded:



          public partial class Window_SelectConnection : Window
          {
          public Window_SelectConnection()
          {
          InitializeComponent();
          Loaded += OnLoaded;
          }

          private void OnLoaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= OnLoaded;
          }
          }


          You may want to create a custom window class that does this for you:



          public class CustomWindow : Window
          {
          public CustomWindow()
          {
          Loaded += CustomWindow_Loaded;
          }

          private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= CustomWindow_Loaded;
          }
          }


          ...and inherit from this one:



          public partial class Window_SelectConnection : CustomWindow

          <local:CustomWindow x:Class="STEP_QuickSound_UI.Window_SelectConnection" ...





          share|improve this answer
























          • I don't know why I did not tried Loaded event. Thanks, that code works.

            – Kevin Kouketsu
            Nov 14 '18 at 15:48














          1












          1








          1







          You can get rid of this behaviour by removing SizeToContent="WidthAndHeight" from your XAML and setting the SizeToContent programmatically once the window has been loaded:



          public partial class Window_SelectConnection : Window
          {
          public Window_SelectConnection()
          {
          InitializeComponent();
          Loaded += OnLoaded;
          }

          private void OnLoaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= OnLoaded;
          }
          }


          You may want to create a custom window class that does this for you:



          public class CustomWindow : Window
          {
          public CustomWindow()
          {
          Loaded += CustomWindow_Loaded;
          }

          private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= CustomWindow_Loaded;
          }
          }


          ...and inherit from this one:



          public partial class Window_SelectConnection : CustomWindow

          <local:CustomWindow x:Class="STEP_QuickSound_UI.Window_SelectConnection" ...





          share|improve this answer













          You can get rid of this behaviour by removing SizeToContent="WidthAndHeight" from your XAML and setting the SizeToContent programmatically once the window has been loaded:



          public partial class Window_SelectConnection : Window
          {
          public Window_SelectConnection()
          {
          InitializeComponent();
          Loaded += OnLoaded;
          }

          private void OnLoaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= OnLoaded;
          }
          }


          You may want to create a custom window class that does this for you:



          public class CustomWindow : Window
          {
          public CustomWindow()
          {
          Loaded += CustomWindow_Loaded;
          }

          private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
          {
          SizeToContent = SizeToContent.WidthAndHeight;
          Loaded -= CustomWindow_Loaded;
          }
          }


          ...and inherit from this one:



          public partial class Window_SelectConnection : CustomWindow

          <local:CustomWindow x:Class="STEP_QuickSound_UI.Window_SelectConnection" ...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 13:10









          mm8mm8

          83.2k81931




          83.2k81931













          • I don't know why I did not tried Loaded event. Thanks, that code works.

            – Kevin Kouketsu
            Nov 14 '18 at 15:48



















          • I don't know why I did not tried Loaded event. Thanks, that code works.

            – Kevin Kouketsu
            Nov 14 '18 at 15:48

















          I don't know why I did not tried Loaded event. Thanks, that code works.

          – Kevin Kouketsu
          Nov 14 '18 at 15:48





          I don't know why I did not tried Loaded event. Thanks, that code works.

          – Kevin Kouketsu
          Nov 14 '18 at 15:48


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299013%2fsizetocontent-issue-with-custom-windowstyle-when-start%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown