2011-12-05 44 views
10

我在應用程序(.NET Framework 4,WPF)中有很多彈出窗口,我必須爲它們設置一種樣式。 實例彈出看起來像這樣:如何將樣式模板寫入Popup控件?

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

回答

22

我建議你包裝一下你的彈出窗口的內容中有點像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> 
+2

我必須添加一些更改才能使此解決方案正常工作,但這個想法正是我所需要的。非常感謝! – Marta

+2

我不得不這樣定義ContentPresenter,否則這個解決方案無法工作。但這個想法本身就是我需要的 - 謝謝!