2010-03-26 134 views
0

我想使用的例子從與資源WPF

http://blogs.microsoft.co.il/blogs/tomershamam/archive/2008/09/22/lt-howto-gt-replace-listview-columns-with-rows-lt-howto-gt.aspx

但是我不想把這個進入App.xaml中的代碼,因爲這將適用於所有GridView的,怎麼做工作我將這個例子應用到應用程序中的幾個gridviews中?參考資料這個樣子

<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> 
         <ScrollBar Name="PART_HorizontalScrollBar" 
           Orientation="Horizontal" 
           Grid.Row="1" 
           Minimum="0.0" 
           Maximum="{TemplateBinding ScrollableWidth}" 
           ViewportSize="{TemplateBinding ViewportWidth}" 
           Value="{Binding Path=HorizontalOffset,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" 
           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" 
           Cursor="Arrow"/> 
         <ScrollBar Name="PART_VerticalScrollBar" 
           Orientation="Vertical" 
           Grid.Column="1" 
           Minimum="0.0" 
           Maximum="{TemplateBinding ScrollableHeight}" 
           ViewportSize="{TemplateBinding ViewportHeight}" 
           Value="{Binding Path=VerticalOffset,RelativeSource={RelativeSource TemplatedParent},Mode=OneWay}" 
           Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" 
           Cursor="Arrow"/> 
         <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> 

    <LinearGradientBrush x:Key="GridViewColumnHeaderBackground" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#FFFFFFFF" Offset="0"/> 
     <GradientStop Color="#FFFFFFFF" Offset="0.4091"/> 
     <GradientStop Color="#FFF7F8F9" Offset="1"/> 
    </LinearGradientBrush> 
    <LinearGradientBrush x:Key="GridViewColumnHeaderBorderBackground" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#FFF2F2F2" Offset="0"/> 
     <GradientStop Color="#FFD5D5D5" Offset="1"/> 
    </LinearGradientBrush> 
    <LinearGradientBrush x:Key="GridViewColumnHeaderHoverBackground" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#FFBDEDFF" Offset="0"/> 
     <GradientStop Color="#FFB7E7FB" Offset="1"/> 
    </LinearGradientBrush> 
    <LinearGradientBrush x:Key="GridViewColumnHeaderPressBackground" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#FF8DD6F7" Offset="0"/> 
     <GradientStop Color="#FF8AD1F5" Offset="1"/> 
    </LinearGradientBrush> 
    <Style x:Key="GridViewColumnHeaderGripper" TargetType="{x:Type Thumb}"> 
     <Setter Property="Canvas.Right" Value="-9"/> 
     <Setter Property="Width" Value="18"/> 
     <Setter Property="Height" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource TemplatedParent}}"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="Background" Value="{StaticResource GridViewColumnHeaderBorderBackground}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Thumb}"> 
        <Border Background="Transparent" Padding="{TemplateBinding Padding}"> 
         <Rectangle Fill="{TemplateBinding Background}" HorizontalAlignment="Center" Width="1"/> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style TargetType="{x:Type GridViewColumnHeader}"> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Background" Value="{StaticResource GridViewColumnHeaderBackground}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource GridViewColumnHeaderBorderBackground}"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="Padding" Value="2,0,2,0"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <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"/> 
         <Canvas> 
          <Thumb x:Name="PART_HeaderGripper" Style="{StaticResource GridViewColumnHeaderGripper}"/> 
         </Canvas> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="Background" TargetName="HeaderBorder" Value="{StaticResource GridViewColumnHeaderHoverBackground}"/> 
          <Setter Property="BorderBrush" TargetName="HeaderHoverBorder" Value="#FF88CBEB"/> 
          <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/> 
          <Setter Property="Background" TargetName="PART_HeaderGripper" Value="Transparent"/> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="true"> 
          <Setter Property="Background" TargetName="HeaderBorder" Value="{StaticResource GridViewColumnHeaderPressBackground}"/> 
          <Setter Property="BorderBrush" TargetName="HeaderHoverBorder" Value="#FF95DAF9"/> 
          <Setter Property="BorderBrush" TargetName="HeaderPressBorder" Value="#FF7A9EB1"/> 
          <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/> 
          <Setter Property="Fill" TargetName="UpperHighlight" Value="#FFBCE4F9"/> 
          <Setter Property="Visibility" TargetName="PART_HeaderGripper" Value="Hidden"/> 
          <Setter Property="Margin" TargetName="HeaderContent" Value="1,1,0,0"/> 
         </Trigger> 
         <Trigger Property="Height" Value="Auto"> 
          <Setter Property="MinHeight" Value="20"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="Role" Value="Floating"> 
       <Setter Property="Opacity" Value="0.4082"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type GridViewColumnHeader}"> 
          <Canvas x:Name="PART_FloatingHeaderCanvas"> 
           <Rectangle Fill="#FF000000" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}" Opacity="0.4697"/> 
          </Canvas> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Trigger> 
      <Trigger Property="Role" Value="Padding"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type GridViewColumnHeader}"> 
          <Border x:Name="HeaderBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,1,0,1"/> 
          <ControlTemplate.Triggers> 
           <Trigger Property="Height" Value="Auto"> 
            <Setter Property="MinHeight" Value="20"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

我曾嘗試創建一個用戶控件,並堅持該批次在UserControl.Resources部分,但它沒有工作,我只能拿這個例子來工作,如果我把它們放進應用我顯然不想要的資源部分。幫幫我!! :-)

+0

您可以發佈嘗試使用UserControl.Resources路由時失敗的代碼(或至少是代碼的摘錄)。 – ChrisF 2010-03-26 12:27:24

+0

它沒有失敗,因爲它只是沒有像代碼在Application.Resources中那樣顯示。 – Coesy 2010-03-26 14:00:04

回答

0

我認爲你的代碼有錯誤。

我只是複製它在測試項目中使用,並得到了一個錯誤:

StaticResource reference 'GridViewColumnHeaderGripper' was not found.

上:

<Style TargetType="{x:Type GridViewColumnHeader}"> 
+0

嘗試使用URL中的示例,這是我從中得到的所有內容,我只是試圖修改它,因此它不適用於所有Gridviews – Coesy 2010-03-26 13:59:02

0

However I don't want to put this into the App.xaml code as this will apply to ALL gridviews, how do I apply this example to a select few gridviews in the application?

只要改變風格的關鍵是比其他的東西GridView.GridViewScrollViewerStyleKey。選擇一個自定義鍵,並在需要它的控件上設置樣式顯式(使用{StaticResource styleKey}

+0

我不認爲它如此簡單:-(我試過了但是它很混亂 – Coesy 2010-03-26 13:58:07

+0

我認爲它*就像那麼簡單......你聲明你的風格的方式,你重寫了默認風格,所以它將被默認風格關鍵字爲GridViewScrollViewerStyleKey的所有ScrollViewer隱式地使用 – 2010-03-26 14:12:27

+0

是啊例如: Style x:Key =「GridViewColumnHeaderGripper」TargetType =「{x:Type Thumb }「 或樣式TargetType =」{x:Type GridViewRowPresenter}「 – Coesy 2010-03-26 14:18:53