WPF Datagrid向datarowheader添加按钮

时间:2013-09-27 13:05:57

标签: c# wpf datagrid

我有一个包含数据网格的WPF应用程序。我正在尝试将切换按钮添加到具有三角形形状的datarowheader。形状应指向右侧,然后一次单击显示行详细信息,箭头应指向下方。

 <!-- The data grid to display orders-->
        <DataGrid DataContext="{Binding OrderBlock}" 
                  x:Name="dataGridOrders" 
                  ItemsSource="{Binding Orders}"
                  Style="{StaticResource DataGridTemplate}"
                  ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"                      
                  RowHeaderStyle="{StaticResource DG_RowHeader}"
                  RowStyle="{StaticResource DG_Row}"
                  CellStyle="{StaticResource DG_Cell}"                      
                  RowDetailsTemplate="{StaticResource DG_RowDetail}"                      
                  AutoGenerateColumns="False"
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch"
                  Background="Silver"
                  RowHeaderWidth="30"                      
                  Margin="25,5,20,15"> 


<!-- Data Grid row header template -->
    <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>
        <Setter Property="Template">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="M"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:1)

您可以使用一个控件模板,其中有一个切换按钮,类似于行标题:

    <Style x:Key="DG_RowHeader" TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Width" Value="35"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                    <ToggleButton Content="button" x:Name="btn"/>
                    <ControlTemplate.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <Binding ElementName="btn" Path="IsChecked"></Binding>
                            </DataTrigger.Binding>
                            <DataTrigger.Setters>
                                <Setter Property="Content" TargetName="btn" Value="-"/>
                            </DataTrigger.Setters>
                        </DataTrigger>
                        <DataTrigger Value="False">
                            <DataTrigger.Binding>
                                <Binding ElementName="btn" Path="IsChecked"></Binding>
                            </DataTrigger.Binding>
                            <DataTrigger.Setters>
                                <Setter Property="Content" TargetName="btn" Value="+"/>
                            </DataTrigger.Setters>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>