gpt4 book ai didi

.net - 如何为 Popup 控件编写样式模板?

转载 作者:行者123 更新时间:2023-12-03 20:11:14 25 4
gpt4 key购买 nike

我在应用程序(.NET Framework 4、WPF)中有很多弹出窗口,我必须为所有这些弹出窗口设置一种样式。
示例弹出窗口如下所示:

<Popup PopupAnimation="Fade" MinWidth="600" MinHeight="200" Placement="Center" VerticalAlignment="Center" HorizontalAlignment="Center" IsEnabled="True" IsOpen="False">
<Grid Width="Auto" Height="Auto" Background="Gray">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border BorderThickness="2" CornerRadius="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="2">
<Border.BorderBrush>
<SolidColorBrush Color="Gray"/>
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush Color="White"/>
</Border.Background>
</Border>

<StackPanel Grid.Row="0">
<Label Foreground="Blue" Content="Popup_Title"/>
</StackPanel>

<GroupBox Grid.Row="1" Header="Popup example content">
<StackPanel>
...
</StackPanel>
</GroupBox>
</Grid>
</Popup>

如何将边框和背景等样式添加到样式模板中?
我无法使用 TargetType Popup 编写 Style 并修改它的 Property="Template"因为弹出控件没有 Property="Template" .那么我该如何为这些弹出窗口编写样式呢?

编辑:
确切的工作作风:
    <Style x:Key="PopupContentStyle" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid Width="Auto" Height="Auto" Background="Gray">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border BorderThickness="2" CornerRadius="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="2">
<Border.BorderBrush>
<SolidColorBrush Color="Gray"/>
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush Color="White"/>
</Border.Background>
</Border>

<StackPanel Grid.Row="0">
<Label Foreground="Blue" Content="Popup_Title"/>
</StackPanel>

<GroupBox Grid.Row="1" Header="Popup example content">
<StackPanel>
<ContentPresenter />
</StackPanel>
</GroupBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

最佳答案

我建议将您的弹出窗口内容包装在类似 ContentControl 的内容中。或 HeaderedContentControl并设置它的风格

<Popup>
<ContentControl Style="{StaticResource PopupContentStyle}">
...
</ContentControl>
</Popup>

示例样式...
<Style x:Key="PopupContentStyle" TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>

<Grid Width="Auto" Height="Auto" Background="Gray">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border BorderThickness="2" CornerRadius="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="2">
<Border.BorderBrush>
<SolidColorBrush Color="Gray"/>
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush Color="White"/>
</Border.Background>
</Border>

<StackPanel Grid.Row="0">
<Label Foreground="Blue" Content="Popup_Title"/>
</StackPanel>

<GroupBox Grid.Row="1" Header="Popup example content">
<StackPanel>
<ContentPresenter />
</StackPanel>
</GroupBox>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

关于.net - 如何为 Popup 控件编写样式模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com