2011-04-04 64 views
1

我已經重寫了默認的行標題風格來使用我的自定義按鈕:WPF覆蓋的DataGrid的rowHeader模板和管理來選擇行仍

<Style x:Key="RowHeaderStyle" TargetType="DataGridRowHeader"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="DataGridRowHeader"> 
       <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Style="{StaticResource RowHeaderButton}" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

當然,問題是,點擊該按鈕不再選擇行。我必須在IsPressed事件上調用自定義方法嗎?我如何知道行索引?你會怎麼做呢?

謝謝!

回答

1

我已經結束了只使用樣式,如果它不是劇烈的,你可以做你想做的事情。如果任何人設法讓DataGridHeaderBorder上的行選擇,請讓我知道! 例如:

<...xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"...> 

<BooleanToVisibilityConverter x:Key="bool2VisibilityConverter"/> 

<LinearGradientBrush x:Key="RowHeaderBackgroundBrush" EndPoint="0.728,0.5" StartPoint="0.272,0.5"> 
    <GradientStop Color="#FF494949" Offset="0"/> 
    <GradientStop Color="#FF3E3E3E" Offset="1"/> 
    <GradientStop Color="#FF494949" Offset="0.50"/> 
    <GradientStop Color="#FF3E3E3E" Offset="0.50"/> 
</LinearGradientBrush> 

<LinearGradientBrush x:Key="RowHeaderBackgroundBrushMouseOver" EndPoint="0.728,0.5" StartPoint="0.272,0.5"> 
    <GradientStop Color="#FF666666" Offset="0"/> 
    <GradientStop Color="#FF525252" Offset="1"/> 
    <GradientStop Color="#FF666666" Offset="0.50"/> 
    <GradientStop Color="#FF525252" Offset="0.50"/> 
</LinearGradientBrush> 

<Style x:Key="RowHeaderGripperStyle" TargetType="{x:Type Thumb}"> 
    <Setter Property="Height" Value="8"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Cursor" Value="SizeNS"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Thumb}"> 
       <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="RowHeaderBorder" TargetType="Border"> 
    <Setter Property="Background" Value="{StaticResource RowHeaderBackgroundBrush}" /> 
    <Setter Property="BorderBrush" Value="#FF313131" /> 
    <Setter Property="BorderThickness" Value="0,0,1,1" /> 
    <Setter Property="CornerRadius" Value="0" /> 
    <Setter Property="Margin" Value="0" /> 

    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="{StaticResource RowHeaderBackgroundBrushMouseOver}"></Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

<Style x:Key="RowHeaderStyle1" TargetType="DataGridRowHeader"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="DataGridRowHeader"> 
       <Grid> 
        <Microsoft_Windows_Themes:DataGridHeaderBorder IsPressed="{TemplateBinding IsPressed}" Orientation="Horizontal" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" Style="{StaticResource RowHeaderBorder}"> 
         <StackPanel Orientation="Horizontal"> 
          <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/> 
          <Control SnapsToDevicePixels="false" Template="{Binding ValidationErrorTemplate, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Visibility="{Binding (Validation.HasError), Converter={StaticResource bool2VisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/> 
         </StackPanel> 
        </Microsoft_Windows_Themes:DataGridHeaderBorder> 
        <Thumb x:Name="PART_TopHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Top"/> 
        <Thumb x:Name="PART_BottomHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Bottom"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
0

是的,你將不得不編碼你自己選擇的事件。

一種方法是使用Blend等工具獲取默認模板的副本,並修改其模板以包含更改。