2011-12-05 12 views
10

Tengo un montón de ventanas emergentes en la aplicación (.NET Framework 4, WPF) y tengo que establecer un estilo para todas ellas. Ejemplo emergente parece que:¿Cómo escribir la plantilla de estilo para el control emergente?

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

¿Cómo puedo estilos como las fronteras y de fondo a una plantilla de estilo? No puedo escribir Estilo con TargetType Popup y modificarlo Property="Template" porque Popup Control no tiene Property="Template". Entonces, ¿cómo puedo escribir estilo en esas ventanas emergentes?

EDIT: estilo de trabajo exacta:

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

Respuesta

22

Me gustaría recomendar envolver el contenido de su emergente en algo así como un ContentControl o una HeaderedContentControl y establecer el estilo de ese

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

Ejemplo estilo ...

<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

Tuve que agregar algunos cambios para que esta solución funcionara, pero esta idea era exactamente lo que necesitaba. ¡Muchas gracias! – Marta

+2

Tuve que definir ContentPresenter así, porque de lo contrario esta solución no funcionaba. Pero la idea en sí es lo que necesito, ¡gracias también!

Cuestiones relacionadas