2011-12-05 16 views
10

Ho un sacco di popup in applicazione (.NET Framework 4, WPF) e devo impostare uno stile per tutti loro. Esempio popup sembra che:Come scrivere il modello di stile sul controllo 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> 

Come posso prendere stili come i bordi e lo sfondo per un modello di stile? Non riesco a scrivere Style con TargetType Popup e modificarlo è Property="Template" perché Popup Control non ha Property="Template". Quindi, come posso scrivere lo stile su quei Popup?

EDIT: stile di lavoro esatta:

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

risposta

22

io consiglierei di avvolgere il contenuto del tuo Popup in qualcosa di simile a un ContentControl o un HeaderedContentControl e impostare lo stile di quel

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

Esempio stile ...

<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

Ho dovuto aggiungere alcune modifiche per far funzionare questa soluzione, ma questa idea era esattamente ciò di cui avevo bisogno. Molte grazie! – Marta

+2

Ho dovuto definire ContentPresenter in questo modo, perché altrimenti questa soluzione non funzionava. Ma l'idea stessa è ciò di cui ho bisogno, grazie anche a me!

Problemi correlati