WPF:我的窗口显示在另一个大窗口内,仅在执行时

hxzsmxv2  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(142)

我创建了一个带有进度条的小“ProgressWindow”。在xaml编辑器,这是确定的,没有问题。但是当我创建了一个窗口并在我的代码中显示它时,ProgressWindow显示在另一个大的白色窗口中。那是什麽?
在xaml编辑器中:

执行中:

Xaml代码:

<Window x:Class="MyStyling.Control.ProgressWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:MyStyling.Control" WindowStyle="none" ResizeMode="NoResize">

<Border  x:Name="Xaml_ProgressWindow_ID" Width="320" BorderThickness="1" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" Background="{DynamicResource MyBackgroundBrush}" VerticalAlignment="Center" Padding="0,0,0,20">
    <Border.Effect>
        <DropShadowEffect Color="Gray" 
                          Direction="300"
                          BlurRadius="10"
                          Opacity="0.5"
                          ShadowDepth="10" />
    </Border.Effect>

    <StackPanel>
        <StackPanel x:Name="XAML_SP_ProgressBar1_ID">
            <Label x:Name="XAML_LBL_ProgressBar1_ID" Content="Main progress bar ..." HorizontalAlignment="Center" />
            <ProgressBar x:Name="XAML_PG_ProgressBar1_ID" Height="5" Margin="20,0,19,0" Background="White" Foreground="Black" />
        </StackPanel>

        <StackPanel x:Name="XAML_SP_ProgressBar2_ID" Margin="0,10,0,0">
            <Label x:Name="XAML_LBL_ProgressBar2_ID" Content="Secondary progress bar" HorizontalAlignment="Center" FontSize="11" />
            <ProgressBar x:Name="XAML_PG_ProgressBar2_ID" Height="5" Margin="20,0,19,0" Background="White" Foreground="Black" />
        </StackPanel>
    </StackPanel>
</Border>
svmlkihl

svmlkihl1#

将窗口的SizeToContent属性设置为WidthAndHeight,以自动调整窗口大小以适合Border

<Window x:Class="MyStyling.Control.ProgressWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:MyStyling.Control" WindowStyle="none" ResizeMode="NoResize"
     SizeToContent="WidthAndHeight">

或者将Border Package 在ViewBox中,以将其拉伸到窗口的大小:

<Window x:Class="MyStyling.Control.ProgressWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:MyStyling.Control" WindowStyle="none" ResizeMode="NoResize"
     Height="200" Width="320">
    <ViewBox>
        <Border x:Name="Xaml_ProgressWindow_ID" BorderThickness="1" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" Background="{DynamicResource MyBackgroundBrush}" VerticalAlignment="Center" Padding="0,0,0,20">
            ...
        </Border>
    </ViewBox>
</Window>

相关问题