【问题标题】:How to write style template to Popup control?如何将样式模板写入 Popup 控件?
【发布时间】:2012-01-13 08:57:49
【问题描述】:

我在应用程序(.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",因为 Popup Control 没有 Property="Template"。那么如何为这些 Popups 编写样式呢?

编辑: 确切的工作方式:

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

【问题讨论】:

    标签: .net wpf xaml popup


    【解决方案1】:

    我建议您将弹出窗口的内容包装在 ContentControlHeaderedContentControl 之类的东西中并设置其样式

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

    【讨论】:

    • 我必须添加一些更改才能使这个解决方案发挥作用,但这个想法正是我所需要的。非常感谢!
    • 我必须像这样定义 ContentPresenter,否则这个解决方案将无法工作。但这个想法本身就是我需要的——也谢谢!
    【解决方案2】:

    我更喜欢 Rachels 一个 Josh Noes 的答案,但遇到了一些麻烦,因为恕我直言,应该在 Style 中定义 ContentControls 属性 Template 而不是 ContentTemplate。将其设置为ControlTemplate(不是DataTemplate)的值,不要忘记它的TargetType。否则,在我的情况下,解决方案不起作用。但真是个绝妙的主意,谢谢。

    
    <Style x:Key="PopupContentStyle" TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                            ...
                            <!--Do some templatestuff around Presenter-->                      
                                   <ContentPresenter />                         
                            <!--/Do some templatestuff around Presenter-->
                       </GroupBox>      
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多