WPF DatagridRowHeader 添加按钮用于某些行而不是所有行

本文关键字:添加 DatagridRowHeader 按钮 用于 WPF | 更新日期: 2023-09-27 18:33:17

我有一个WPF数据网格。数据网格绑定到名为"订单"的自定义类型的列表。目前,我为数据网格中的每一行都有一个切换按钮。

如果订单的属性 MultiID(布尔值)为真但无法使其正常工作,我只希望在行标题中有一个切换按钮。

在我的 DataGridRow 中,我尝试使用下面一行中注释掉的部分,这似乎没有区别,因为每一行都有一个切换按钮。

<Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Blue"/>
            </Trigger>
            <!--<DataTrigger Binding="{Binding MultiID}" Value="True">
                <Setter Property="HeaderTemplate" Value="{StaticResource rowdetailToggleButton}"/>
            </DataTrigger>-->
        </Style.Triggers>

我的数据网格

<!-- The data grid to display orders-->
        <DataGrid DataContext="{Binding OrderBlock}" 
                  x:Name="dataGridOrders" 
                  ItemsSource="{Binding Orders}"
                  RowStyle="{StaticResource DG_Row}"
                  RowDetailsTemplate="{StaticResource DG_RowDetail}" 
                  RowHeaderStyle="{StaticResource DG_RowHeader}"
                                        RowDetailsVisibilityChanged="dataGridOrders_RowDetailsVisibilityChanged">
                <DataGrid.RowHeaderTemplate>                    
                <DataTemplate>
                    <ToggleButton x:Name="RowHeaderToggleButton"                                                                           
                                  Click="RowHeaderToggleButton_Click"
                                  Cursor="Hand"/>
   <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Orders.MultiID}" Value="False">
                                <Setter TargetName="RowHeaderToggleButton" Property="Visibility" Value="Collapsed"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                 </DataGrid.RowHeaderTemplate>

My 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>
                        <ContentPresenter Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"/>                            
                    </Border>                        
                </ControlTemplate>
                                </Setter.Value>
        </Setter>
    </Style>

My DataGridRow

 <!-- Data grid formatting Grid Row template -->
    <Style x:Key="DG_Row" TargetType="{x:Type DataGridRow}">
        <Setter Property="Opacity" Value="1"/>
        <Setter Property="Padding" Value="3,2,2,3"/>            
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border x:Name="DGR_Border"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="True">
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Offset="0" Color="Transparent"/>
                                <GradientStop Offset="1" Color="Silver"/>
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding StatusGood}" Value="Send">
                            <Setter TargetName="DGR_Border" Property="Background" Value="{StaticResource brushRowSend}"/>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Blue"/>
            </Trigger>
            <!--<DataTrigger Binding="{Binding MultiID}" Value="True">
                <Setter Property="HeaderTemplate" Value="{StaticResource rowdetailToggleButton}"/>
            </DataTrigger>-->
        </Style.Triggers>
    </Style>

WPF DatagridRowHeader 添加按钮用于某些行而不是所有行

您可以将 DataTrigger 放在为 RowHeaderTemplate 定义的 DataTemplate 中

      <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding DataContext.MultiID,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}" Value="false">
                <Setter TargetName="RowHeaderToggleButton" Property="Visibility" Value="Collapsed"/>
             </DataTrigger>
       </DataTemplate.Triggers>

您可以使用 .Net 内置 IValueConverter 本身。

将此行添加到资源中

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

并像这样在您的按钮上使用它

<DataGrid.RowHeaderTemplate>                    
    <DataTemplate>
         <ToggleButton x:Name="RowHeaderToggleButton" 
                       Visibility="{Binding MultiID, Converter={StaticResource BooleanToVisibilityConverter}}"
                       Click="RowHeaderToggleButton_Click"
                       Cursor="Hand"/>
    </DataTemplate>
</DataGrid.RowHeaderTemplate>