XAML 具有堆栈面板和不遵循列规则的触发器的列

pbgvytdp  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(112)

它已经超过5年,因为我最后一次做任何事情与WPF和我有点生 rust 。也就是说,我似乎过早地跳进了深渊,做了一些我想不通的事情。
我有一个分层的DataGrid,工作得很好,除了列不太遵循行中所有其他列遵循的规则,即当一行被选中时,该行中的所有列都显示不同的背景颜色来指示所选行,除了第二列,它继续显示行/备用行颜色模式,就好像它不在所选行中一样。有人能指出我错在哪里吗?

<Style TargetType="DataGridColumnHeader">
            <Setter Property="Background" Value="#f5f5f5"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="1,0,0,1"/>
            <Setter Property="SeparatorBrush" Value="Black"/>
            <Setter Property="Foreground" Value="#6e6e6e"/>
            <Setter Property="Padding" Value="5,0,0,0"/>
        </Style>
        <Style TargetType="DataGrid">
            <Setter Property="VerticalGridLinesBrush" Value="#6e6e6e"/>
            <Setter Property="HorizontalGridLinesBrush" Value="#6e6e6e"/>
            <Setter Property="AlternatingRowBackground" Value="#f5f5f5"/>
            <Setter Property="RowBackground" Value="White"/>
            <Setter Property="RowHeight" Value="25" />
            <Setter Property="RowHeaderWidth" Value="0"/>
            <Setter Property="Foreground" Value="#6e6e6e"/>
            <Setter Property="Background" Value="#ffffff"/>
            <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Padding" Value="10,0,0,0"/>
        </Style>
        <Style x:Key="CommonColumnElementStyle" TargetType="TextBlock">
            <Setter Property="Margin" Value="5,0,0,0"/>
            <Setter Property="Foreground" Value="Black"/>
        </Style>        
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <local:IndentBasedOnLevel x:Key="IndentBasedOnLevel"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#316dff"/>
        </Grid.Resources>
        <DataGrid x:Name="HierarchialDataGrid" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" GridLinesVisibility="All" ColumnWidth="*">
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="40" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Device" Width="*" Binding="{Binding Data.Name}" ElementStyle="{StaticResource CommonColumnElementStyle}">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="Background" Value="White"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="DataGridCell">
                                        <StackPanel Orientation="Horizontal">
                                            <ToggleButton x:Name="Expander" 
                                                          Margin="{Binding Level,Converter={StaticResource IndentBasedOnLevel}}"
                                                          IsChecked="{Binding Path=IsExpanded, UpdateSourceTrigger=PropertyChanged}" ClickMode="Press">
                                                <ToggleButton.Style>
                                                    <Style TargetType="{x:Type ToggleButton}">
                                                        <Setter Property="Focusable" Value="False"/>
                                                        <Setter Property="Width" Value="19"/>
                                                        <Setter Property="Height" Value="13"/>
                                                        <Setter Property="Template">
                                                            <Setter.Value>
                                                                <ControlTemplate TargetType="{x:Type ToggleButton}">
                                                                    <Border Width="19" Height="13" Background="Transparent">
                                                                        <Border Width="11" Height="11"
                                                                              BorderThickness="0"
                                                                              BorderBrush="#FF7898B5"
                                                                              CornerRadius="1"
                                                                              SnapsToDevicePixels="true">                                                                            
                                                                            <Path x:Name="ExpandPath"
                                                                            Margin="1,1,1,1"
                                                                            Fill="Black"
                                                                            Data="M 0 0 L 9 5 L 0 9 Z"/>                                                                            
                                                                        </Border>
                                                                    </Border>
                                                                    <ControlTemplate.Triggers>                                                                        
                                                                        <Trigger Property="IsChecked"
                                                                            Value="True">
                                                                            <Setter Property="RenderTransform"
                                                                                TargetName="ExpandPath">
                                                                                <Setter.Value>
                                                                                    <RotateTransform Angle="180"
                                                                                     CenterY="3"
                                                                                     CenterX="3" />
                                                                                </Setter.Value>
                                                                            </Setter>
                                                                            <Setter Property="Fill"
                                                                                TargetName="ExpandPath"
                                                                                Value="Black" />                                                                                                                                                      
                                                                            <Setter Property="Data"
                                                                            TargetName="ExpandPath"
                                                                            Value="M 0 7 L 7 0 L 0 0 Z"/>                                                                            
                                                                        </Trigger>
                                                                    </ControlTemplate.Triggers>
                                                                </ControlTemplate>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </Style>
                                                </ToggleButton.Style>
                                            </ToggleButton>
                                            <ContentPresenter Content="{TemplateBinding Content}" />
                                        </StackPanel>
                                        <ControlTemplate.Triggers>
                                            <DataTrigger Binding="{Binding HasChildren}" Value="False">
                                                <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>                                                
                                            </DataTrigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="Alias" Width="*" Binding="{Binding Data.Alias}" ElementStyle="{StaticResource CommonColumnElementStyle}"/>
                <DataGridTextColumn Header="Deplyment Group" Width="*" Binding="{Binding Data.DeploymentGroup}" ElementStyle="{StaticResource CommonColumnElementStyle}"/>
                <DataGridTextColumn Header="Is Configured  " Width="Auto" Binding="{Binding Data.IsConfigured}" ElementStyle="{StaticResource CommonColumnElementStyle}"/>
                <DataGridTextColumn Header="Transfer Configuration" Width="*" Binding="{Binding Data.TransferConfiguration}" ElementStyle="{StaticResource CommonColumnElementStyle}"/>
                <DataGridTextColumn Header="Transfer Status  " Width="*" Binding="{Binding Data.TransferStatus}" ElementStyle="{StaticResource CommonColumnElementStyle}"/>
                <DataGridTextColumn Header="Host Status  " Width="Auto" Binding="{Binding Data.HostStatus}" ElementStyle="{StaticResource CommonColumnElementStyle}"/>
                <DataGridTextColumn Header="Last Transfer Update" Width="*" Binding="{Binding Data.LastTransferUpdate}" ElementStyle="{StaticResource CommonColumnElementStyle}"/>
            </DataGrid.Columns>
        </DataGrid>        
    </Grid>```
djmepvbi

djmepvbi1#

有两种颜色可能会覆盖选定的行颜色:
Yout ToggleButton模板有一个透明的边框:

<Border Width="19" Height="13" Background="Transparent">

此外,对于DataGridCell,您要为该单元格指定背景颜色(白色):

<DataGridTextColumn Header="Device" Width="*" Binding="{Binding Data.Name}" ElementStyle="{StaticResource CommonColumnElementStyle}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="White"/>

相关问题