1

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

如果 Orders 的属性 MultiID (boolean) 为 true 但无法使其正常工作,我只希望在 rowheader 中有一个切换按钮。

在我的 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>

我的 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>

我的数据网格行

 <!-- 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>
4

2 回答 2

0

您可以使用 .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>
于 2013-09-30T23:09:15.033 回答
0

您可以将 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>
于 2013-09-30T14:57:41.980 回答