2010-03-09 104 views
1

我一直在這個時候把我的頭髮撕掉,希望有人能幫助我。在這一個承擔我。將樣式應用到gridview中

我有一些樣式在我的App.xaml中轉換gridview,代碼看起來像這樣。

<Application.Resources> 

    <Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" 
     TargetType="{x:Type ScrollViewer}"> 
     <Setter Property="Focusable" 
      Value="false"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ScrollViewer}"> 
        <Grid Background="{TemplateBinding Background}" 
         SnapsToDevicePixels="true"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition Width="Auto"/> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="Auto"/> 
         </Grid.RowDefinitions> 
         <DockPanel Margin="{TemplateBinding Padding}"> 
          <ScrollViewer DockPanel.Dock="Left" 
             HorizontalScrollBarVisibility="Hidden" 
             VerticalScrollBarVisibility="Hidden" 
             Focusable="false"> 
           <GridViewHeaderRowPresenter Margin="2,0,2,0" 
                 Columns="{Binding Path=TemplatedParent.View.Columns,RelativeSource={RelativeSource TemplatedParent}}" 
                 ColumnHeaderContainerStyle="{Binding Path=TemplatedParent.View.ColumnHeaderContainerStyle,RelativeSource={RelativeSource TemplatedParent}}" 
                 ColumnHeaderTemplate="{Binding Path=TemplatedParent.View.ColumnHeaderTemplate,RelativeSource={RelativeSource TemplatedParent}}" 
                 ColumnHeaderTemplateSelector="{Binding Path=TemplatedParent.View.ColumnHeaderTemplateSelector,RelativeSource={RelativeSource TemplatedParent}}" 
                 AllowsColumnReorder="{Binding Path=TemplatedParent.View.AllowsColumnReorder,RelativeSource={RelativeSource TemplatedParent}}" 
                 ColumnHeaderContextMenu="{Binding Path=TemplatedParent.View.ColumnHeaderContextMenu,RelativeSource={RelativeSource TemplatedParent}}" 
                 ColumnHeaderToolTip="{Binding Path=TemplatedParent.View.ColumnHeaderToolTip,RelativeSource={RelativeSource TemplatedParent}}" 
                 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </ScrollViewer> 
          <ScrollContentPresenter Name="PART_ScrollContentPresenter" 
            KeyboardNavigation.DirectionalNavigation="Local" 
            Content="{TemplateBinding Content}" 
            ContentTemplate="{TemplateBinding ContentTemplate}" 
            CanContentScroll="{TemplateBinding CanContentScroll}" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </DockPanel> 

         <DockPanel Grid.Column="1" 
           Grid.Row="1" 
           Background="{Binding Path=Background,ElementName=PART_VerticalScrollBar}" 
           LastChildFill="false"> 
          <Rectangle DockPanel.Dock="Left" 
            Width="1" 
            Fill="White" 
            Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/> 
          <Rectangle DockPanel.Dock="Top" 
            Height="1" 
            Fill="White" 
            Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/> 
         </DockPanel> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <Style TargetType="{x:Type GridViewHeaderRowPresenter}"> 
     <Setter Property="Height" Value="80" /> 
     <Setter Property="LayoutTransform"> 
      <Setter.Value> 
       <TransformGroup> 
        <RotateTransform Angle="-90" /> 
        <ScaleTransform ScaleY="-1" /> 
       </TransformGroup> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <Style TargetType="{x:Type GridViewRowPresenter}"> 
     <Setter Property="LayoutTransform"> 
      <Setter.Value> 
       <TransformGroup> 
        <RotateTransform Angle="-90" /> 
        <ScaleTransform ScaleY="-1" /> 
       </TransformGroup> 
      </Setter.Value> 
     </Setter> 
    </Style> 



    <Style TargetType="{x:Type ListView}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListView"> 
        <Border Name="Border" BorderThickness="1" BorderBrush="Black" Background="White"> 
         <ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}"> 
          <ItemsPresenter /> 
         </ScrollViewer> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsGrouping" Value="true"> 
          <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter TargetName="Border" Property="Background" Value="White"/> 

         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 

      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style TargetType="{x:Type GridViewColumnHeader}"> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="Padding" Value="2,0,2,0"/> 
     <Setter Property="Template"> 
      <Setter.Value> 

       <ControlTemplate TargetType="{x:Type GridViewColumnHeader}"> 
        <Grid SnapsToDevicePixels="true"> 
         <Border x:Name="HeaderBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,1,0,1"> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition MaxHeight="7"/> 
            <RowDefinition/> 
           </Grid.RowDefinitions> 
           <Rectangle Fill="#FFE3F7FF" x:Name="UpperHighlight" Visibility="Collapsed"/> 
           <Border Grid.RowSpan="2" Padding="{TemplateBinding Padding}"> 
            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0,0,0,1" x:Name="HeaderContent" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"> 
             <ContentPresenter.LayoutTransform> 
              <TransformGroup> 
               <ScaleTransform ScaleY="-1" /> 
               <RotateTransform Angle="90" /> 
              </TransformGroup> 
             </ContentPresenter.LayoutTransform> 
            </ContentPresenter> 
           </Border> 
          </Grid> 
         </Border> 
         <Border Margin="1,1,0,0" x:Name="HeaderHoverBorder" BorderThickness="1,0,1,1"/> 
         <Border Margin="1,0,0,1" x:Name="HeaderPressBorder" BorderThickness="1,1,1,0"/> 

        </Grid> 
        <ControlTemplate.Triggers> 

         <Trigger Property="Height" Value="Auto"> 
          <Setter Property="MinHeight" Value="20"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="Black"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 

    </Style> 
</Application.Resources> 

現在,這適用於我的網頁上的所有gridviews,我將如何使這些樣式適用於某些gridviews而不是其他人?我已經嘗試了一百萬種不同的方式,並且我確信我錯過了一些明顯的東西!,請幫助:-)

回答

0

您必須將Key設置爲一個字符串,如<Style x:Key="myGridStyle" TargetType="{x:Type ScrollViewer}">,然後調用該樣式作爲所需的網格使用它。另外,如果你只設置了一個TargetType而不是一個Key,那麼它就是這個Type的默認值,除非你覆蓋它。

+0

@Smoore我的確在想這件事,但是我必須將很多密鑰添加到一個網格中,難道沒有辦法將它們全部集中到一個密鑰中嗎? – Coesy 2010-03-09 16:17:36

+0

是的,就是。也許你可以把這個Gird做成一個UserControl,然後用你喜歡的方式構建這個Grid,然後在整個項目中隨時隨地使用這個控件。 – 2010-03-09 16:19:56

+0

由於它們是單獨的控件,因此它們都必須具有單獨的鍵,因此將它們組合成一個鍵的唯一方法是將它們全部組合到一個控件中。所有這些控件都是ListView控件的一部分嗎?如果是這樣,爲什麼你不能只擁有一個ListView風格,並在其中定義了所有這些東西? – 2010-03-09 16:28:33