如何在 TabControl 上设置椭圆样式触发器以在 DataGrids 有选择时突出显示

How to set Ellipse style trigger on TabControl to highlight when DataGrids has selection

我在每个 TabControl header 中都有一个 Ellipse ,如果在它下面的 DataGrid 中选择了任何项目,则它会突出显示蓝色,否则应该是透明的。目前我有 Trigger 相反,所以如果 DataGrid SelectedItems.Count 是 0 它是透明的,如果有某种方式将默认设置为透明那会很好。

注意:它将有多个选项卡并关联 DataGrids

如何让 Style TriggerEllipse 上正常工作?

<TabControl Name="tcGeometry" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding GEOMETRIES}" >
    <TabControl.ItemTemplate>
        <DataTemplate>
            <Grid>                            
                <TextBlock Text="{Binding DISPLAY_NAME}" Margin="0,0,25,0"/>
                <Ellipse x:Name="SelectionIndicator"
                         Width="8" Height="8"
                         Stroke="Black"
                         Fill="Blue"
                         StrokeThickness="1"
                         HorizontalAlignment="Right"
                         VerticalAlignment="Top">
                    <Ellipse.Style>
                        <Style TargetType="{x:Type Ellipse}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding  SelectedItems.Count, ElementName=dgAudit}"  Value="0">
                                    <Setter Property="Fill" Value="Transparent" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Ellipse.Style>
                </Ellipse>
            </Grid>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <Grid ShowGridLines="False">
                <DataGrid Name="dgAudit" 
                          Grid.Row="0"
                          Grid.Column="0"
                          IsReadOnly="True"
                          ItemsSource="{Binding GEOM_ASSET_OC_LIST}"
                          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
                          AutoGenerateColumns="False"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          RowDetailsVisibilityMode="Collapsed"
                          RowHeaderWidth="30" />
            </Grid>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

不要为 EllipseFill 属性 设置本地值。在 Style setter.

中设置默认值
<Ellipse x:Name="SelectionIndicator"
                         Width="8" Height="8"
                         Stroke="Black"
                         StrokeThickness="1"
                         HorizontalAlignment="Right"
                         VerticalAlignment="Top">
    <Ellipse.Style>
        <Style TargetType="{x:Type Ellipse}">
            <Setter Property="Fill" Value="Blue" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedItems.Count, ElementName=dgAudit}" Value="0">
                    <Setter Property="Fill" Value="Transparent" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Ellipse.Style>
</Ellipse>

本地值优先于样式中设置的值:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence

您可能还必须绑定到 源集合 Count 属性,它跟踪所选项目而不是绑定到 SelectedItems 属性 DataGrid 元素。这是因为 TabControl 默认情况下会在您切换选项卡时卸载取消选择选项卡的可视化树:

How to stop Wpf Tabcontrol to unload Visual tree on Tab change