我使用设置模板属性为类型Window创建了样式。如果我为窗口设置高度和宽度属性,它工作正常。但是当我试图设置SizeToContent =“WidthAndHeight”时,窗口的高度和宽度被设置为屏幕的全高和宽度(即使窗口的所有内容都受到高度和宽度的限制)。可以在控件模板中处理SizeToContent吗?
这是窗口样式:
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="AllowsTransparency" Value="true" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="ResizeMode" Value="CanResizeWithGrip" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid
Margin="7"
TextOptions.TextRenderingMode="ClearType"
TextOptions.TextFormattingMode="Display"
Background="Transparent">
<Border
x:Name="PART_Border"
Width="Auto"
Height="Auto"
Background="#EFEFF2"
BorderBrush="{StaticResource AppBrush}"
BorderThickness="1"
Padding="0"
CornerRadius="5">
<Border.Effect>
<DropShadowEffect
Color="black"
Opacity="0.5"
BlurRadius="7"
ShadowDepth="2"
Direction="315"/>
</Border.Effect>
<DockPanel
HorizontalAlignment="Stretch"
Background="Transparent"
VerticalAlignment="Stretch"
>
<Border
x:Name="TitleBar"
DockPanel.Dock="Top"
Background="#EFEFF2"
BorderThickness="0"
MouseLeftButtonDown="TitleBarMouseLeftButtonDown"
MouseMove="TitleBarMouseMove"
CornerRadius="5,5,0,0"
>
<Grid Height="35">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="36"/>
<ColumnDefinition />
<ColumnDefinition Width="26"/>
<ColumnDefinition Width="26"/>
<ColumnDefinition Width="26"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<Image
x:Name="Icon"
Grid.Column="0"
Source="{Binding Path=Icon, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
HorizontalAlignment="Right"
Margin="4,-7,0,7"
Width="32"
Height="32"
MouseLeftButtonDown="IconMouseLeftButtonDown"
/>
<Label
Grid.Column="1"
VerticalAlignment="Center"
Content="{Binding Path=Title, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" FontFamily="FuturaDemiC" FontSize="18">
</Label>
<Button x:Name="MinButton"
Grid.Column="2"
Width="20"
Height="20"
Style="{StaticResource ImageButton}"
Click="MinButtonClick"
>
<Image Source="{StaticResource App_Minimize}"/>
</Button>
<Button x:Name="MaxButton"
Grid.Column="3"
Width="20"
Height="20"
Style="{StaticResource ImageButton}"
Click="MaxButtonClick"
>
<Image Name="MaxButtonImage"
Source="{StaticResource App_Maximize}" />
</Button>
<Button x:Name="CloseButton"
Grid.Column="4"
Width="20"
Height="20"
Style="{StaticResource ImageButton}"
Click="CloseButtonClick"
>
<Image Source="{StaticResource App_Close}" />
</Button>
</Grid>
</Border>
<ContentPresenter />
</DockPanel>
</Border>
<Line
MouseDown="OnSizeNorth"
x:Name="lnSizeNorth"
Stroke="Transparent"
Cursor="SizeNS"
X1="1" X2="{TemplateBinding ActualWidth}" Y1="1" Y2="1"
StrokeThickness="3"
/>
<Line
MouseDown="OnSizeSouth"
x:Name="lnSizeSouth"
Stroke="Transparent"
VerticalAlignment="Bottom"
Cursor="SizeNS"
X1="1" X2="{TemplateBinding ActualWidth}" Y1="{TemplateBinding ActualHeight}" Y2="{TemplateBinding ActualHeight}"
StrokeThickness="3"
/>
<Line
MouseDown="OnSizeWest"
x:Name="lnSizeWest"
Stroke="Transparent"
Cursor="SizeWE"
X1="1" X2="1" Y1="1" Y2="{TemplateBinding ActualHeight}"
StrokeThickness="3"
/>
<Line
MouseDown="OnSizeEast"
x:Name="lnSizeEast"
Stroke="Transparent"
HorizontalAlignment="Right"
Cursor="SizeWE"
X1="{TemplateBinding ActualWidth}" X2="{TemplateBinding ActualWidth}" Y1="1" Y2="{TemplateBinding ActualHeight}"
StrokeThickness="3"
/>
<Rectangle MouseDown="OnSizeNorthWest" x:Name="rectSizeNorthWest" Cursor="SizeNWSE" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Top" HorizontalAlignment="Left" />
<Rectangle MouseDown="OnSizeNorthEast" x:Name="rectSizeNorthEast" Cursor="SizeNESW" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Top" HorizontalAlignment="Right" />
<Rectangle MouseDown="OnSizeSouthWest" x:Name="rectSizeSouthWest" Cursor="SizeNESW" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
<Rectangle MouseDown="OnSizeSouthEast" x:Name="rectSizeSouthEast" Cursor="SizeNWSE" Fill="Transparent" Width="5" Height="5" VerticalAlignment="Bottom" HorizontalAlignment="Right" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="MaxButtonImage" Property="Source" Value="{StaticResource App_Maximize}"/>
</Trigger>
<Trigger Property="IsActive" Value="False">
<Setter TargetName="PART_Border" Property="BorderBrush" Value="{StaticResource WindowBorderBrushInactive}"/>
</Trigger>
<Trigger Property="ResizeMode" Value="NoResize">
<Setter TargetName="MinButton" Property="Visibility" Value="Hidden"/>
<Setter TargetName="MaxButton" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Resources>
<Style TargetType="{x:Type StatusBar}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive, RelativeSource={RelativeSource AncestorType=Window}}" Value="True">
<Setter Property="Foreground" Value="{StaticResource WindowStatusForeground}" />
<Setter Property="Background" Value="{StaticResource WindowBorderBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsActive, RelativeSource={RelativeSource AncestorType=Window}}" Value="False">
<Setter Property="Foreground" Value="{StaticResource WindowStatusForegroundInactive}" />
<Setter Property="Background" Value="{StaticResource WindowBorderBrushInactive}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
答案 0 :(得分:1)
看起来您遇到的问题是内容没有大小,因此它占用了最大可用空间,对我来说,它将宽度设置为我的两个屏幕的组合宽度。
你基本上有这样一种情况:窗口会说“我会尽可能多地接受你需要的内容”,而内容却说“我会尽可能多地给你一个窗口”,所以Window正在服用这一切。
编辑:实际上,如果要删除包含网格上的边距,然后删除具有ActualHeight / Width绑定的行,那么它将解决您的问题。看起来模板的这两个方面正在推出尺寸,直到达到最大值。