2011-12-05 17 views
10

Uygulamada çok fazla pop-up var (.NET Framework 4, WPF) ve hepsine bir stil ayarlamak zorundayım. Örnek açılan benziyor:Popup kontrolüne stil şablonu nasıl yazılır?

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

Nasıl bir stil şablonu sınırlar ve arka gibi stilleri alabilir? TargetType Popup ile Style yazamıyorum ve Property="Template" değerini değiştiremiyorum, çünkü Popup Control'ün Property="Template" olması gerekmez. Peki bu Popup'lara nasıl stil yazabilirim?

DÜZENLEME: Tam çalışma stili:

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

cevap

22

O

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

Örnek stilini bir ContentControl veya HeaderedContentControl gibi bir şey içinde Popup İçeriğini sarma ve ayar öneriyoruz stil ...

<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

Bu çözümü çalıştırmak için bazı değişiklikler eklemeliydim, ancak bu fikir tam ihtiyacım olan şeydi. Çok teşekkürler! – Marta

+2

ContentPresenter'ı bunun gibi tanımlamak zorundaydım çünkü aksi halde bu çözüm işe yaramadı. Ama fikrin kendisi ihtiyacım olan şey - teşekkürler!