Dot-Net

如何將樣式模板寫入 Popup 控制項?

  • December 17, 2020

我在應用程序(.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". 那麼如何為這些彈出視窗編寫樣式呢?

編輯: 確切的工作方式:

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

我建議將您的彈出視窗的內容包裝成類似 aContentControl或 aHeaderedContentControl並設置其樣式

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

引用自:https://stackoverflow.com/questions/8388365