2013-09-30 74 views
1

我在我的WPF應用程序中有一個datagrid。我一直在試圖添加一個切換按鈕到datagridrowheader,我已經做到了。我遇到的問題是試圖設置datagridrowheader的樣式。如果我省略了行 - RowHeaderStyle =「{StaticResource DG_RowHeader}」我的按鈕加載正確。當我添加這條線時,我的按鈕不會出現,爲什麼?WPF Datagrid RowHeaderTemplate被RowHeaderStyle覆蓋

據我可以看到DG_RowHeader只是樣式行頭?我想知道如何將這種樣式應用到我的datagridrowheader並讓我的切換按鈕也出現?

我的DataGrid

<DataGrid DataContext="{Binding OrderBlock}" 
        x:Name="dataGridOrders" 
        ItemsSource="{Binding Orders}" 
        Style="{StaticResource DataGridTemplate}" 
        ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"            
        RowStyle="{StaticResource DG_Row}" 
        CellStyle="{StaticResource DG_Cell}"    
        RowHeaderStyle="{StaticResource DG_RowHeader}" 
        RowDetailsTemplate="{StaticResource DG_RowDetail}"      
        AutoGenerateColumns="False" 
        HorizontalAlignment="Stretch" 
        VerticalAlignment="Stretch" 
        Background="Silver" 
        RowHeaderWidth="30"      
        Margin="25,5,20,15" 
        RowDetailsVisibilityChanged="dataGridOrders_RowDetailsVisibilityChanged"> 
      <DataGrid.RowHeaderTemplate>      
       <DataTemplate> 
        <ToggleButton x:Name="RowHeaderToggleButton"          
            Click="RowHeaderToggleButton_Click" 
            Cursor="Hand"/> 
       </DataTemplate> 
      </DataGrid.RowHeaderTemplate> 

我DataGridRowHeader

<!-- Data Grid row with toggle button --> 
    <Style x:Key="DG_RowHeader" TargetType="{x:Type DataGridRowHeader}"> 
     <Setter Property="Width" Value="35"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type DataGridRowHeader}"> 
        <Border x:Name="DGRH_Border" 
           BorderBrush="{TemplateBinding BorderBrush}" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           SnapsToDevicePixels="True"> 
         <Border.Background> 
          <LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> 
           <GradientStop Offset="0" Color="LightGray"/> 
           <GradientStop Offset="1" Color="WhiteSmoke"/> 
          </LinearGradientBrush> 
         </Border.Background> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

我的切換按鈕

<!-- Toogle Button --> 
    <Style TargetType="ToggleButton" x:Name="rowdetailToggleButton"> 
     <Setter Property="Padding" Value="0" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ToggleButton"> 
        <Grid>        
         <Path x:Name="DefaultPath" 
          VerticalAlignment="Top" 
          Data="M0,0 14,7 0,14 Z" 
          Fill="DarkGray" 
          Stretch="Fill" 
          Margin="6"/> 
         <Path x:Name="CheckedPath" 
          VerticalAlignment="Top" 
          Data="M0,0 14,0 7,14 Z" 
          Fill="DarkGray" 
          Stretch="Fill" 
          Margin="6" 
          Visibility="Collapsed" /> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CheckStates"> 
           <VisualState x:Name="Checked"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Duration="0" 
                     Storyboard.TargetName="DefaultPath" 
                     Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Collapsed</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Duration="0" 
                     Storyboard.TargetName="CheckedPath" 
                     Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Visible</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
             <ColorAnimationUsingKeyFrames Storyboard.TargetName="CheckedPath" Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"> 
              <SplineColorKeyFrame KeyTime="0:0:0.2" Value="LightGray" /> 
             </ColorAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unchecked" /> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

回答

1

它看起來像你忘了顯示DataGridRowHeaderDG_RowHeader風格的內容,更確切地說裏面DGRH_Border

您可以添加一個ContentPresenter它裏面:

<Border x:Name="DGRH_Border" 
     BorderBrush="{TemplateBinding BorderBrush}" 
     BorderThickness="{TemplateBinding BorderThickness}" 
     SnapsToDevicePixels="True"> 
    [...] 
    <ContentPresenter Content="{TemplateBinding Content}" 
        ContentTemplate="{TemplateBinding ContentTemplate}"/> 
</Border> 

至於ToggleButtons,你需要:

  • 指定的ToggleButton的聲明風格:

(假設你的風格是使用x:Key屬性(您指定的x:Name代替)定義):

<Style TargetType="ToggleButton" x:Key="rowdetailToggleButton"> 
  • 設置樣式爲的ToggleButtons默認樣式通過移除x:Key屬性:

無論哪種方式,你需要確保你的資源的dic從按鈕的聲明中可以找到它們。

+0

嗨Franssu我已經添加了該行,但似乎沒有任何區別 - 仍然看不到切換按鈕? – mHelpMe

+0

我想我忘了ContentTemplate =「{TemplateBinding ContentTemplate}」 – franssu

+0

現在顯示切換按鈕,但樣式不見了? – mHelpMe