启动时自定义windowstyle的SizeToContent问题

时间:2018-11-14 11:19:41

标签: c# wpf

我的问题是:我有两种尺寸的窗口,它们根据CheckBox而变化。以下是问题的屏幕截图:

enter image description here

就像您看到的那样,我的应用程序以不正确的大小开始,但是当我更改复选框时,一切正常,窗口大小变为正常

enter image description here enter image description here

我已经测试过在应用程序启动时将CheckBox设置为由后面的代码启动并且无法正常工作。

这是我的自定义windowstyle的XAML

    <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代码

<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>

添加:

如果我删除了自定义窗口样式,则可以正常工作,所以这是我所缺少的。

1 个答案:

答案 0 :(得分:1)

您可以通过从XAML中删除SizeToContent="WidthAndHeight"并在加载窗口后以编程方式设置SizeToContent来摆脱此行为:

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

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

您可能想要创建一个自定义窗口类来为您执行此操作:

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

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

...并从此继承:

public partial class Window_SelectConnection : CustomWindow

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