2011-12-05 16 views
10

Tôi có rất nhiều cửa sổ bật lên trong ứng dụng (.NET Framework 4, WPF) và tôi phải đặt một kiểu cho tất cả chúng. Ví dụ về cửa sổ bật lên trông giống như sau:Làm thế nào để viết mẫu kiểu để điều khiển 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> 

Làm cách nào để tạo kiểu như đường viền và nền cho mẫu kiểu? Tôi không thể viết Style với PopType TargetType và sửa đổi nó là Property="Template" vì Popup Control không có Property="Template". Vậy làm thế nào tôi có thể viết phong cách cho những Popups?

EDIT: phong cách làm việc chính xác:

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

Trả lời

22

tôi khuyên bạn nên gói nội dung của Popup của bạn trong một cái gì đó giống như một ContentControl hoặc một HeaderedContentControl và thiết lập các phong cách đó

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

Ví dụ phong cách ...

<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

Tôi phải thêm một số thay đổi để làm cho giải pháp này hoạt động, nhưng ý tưởng này chính xác là những gì tôi cần. Cảm ơn rất nhiều! – Marta

+2

Tôi đã phải xác định ContentPresenter như thế này, bởi vì nếu không giải pháp này không hoạt động. Nhưng chính ý tưởng đó là những gì tôi cần - cảm ơn!