如何将样式模板写入Popup控件?

时间:2011-12-05 16:22:17

标签: .net wpf xaml popup

我在应用程序(.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"。那么如何为那些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>

2 个答案:

答案 0 :(得分:24)

我建议将Popup的内容包含在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>

答案 1 :(得分:0)

我更喜欢雷切尔(Rachels)和乔什·诺斯(Josh Noes)的答案,但遇到了一些麻烦,因为恕我直言,应该以样式而不是ContentControl来定义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>