0

I have an Ellipse in each TabControl header which is to highlight blue if there is any items selected in the DataGrid below it, otherwise it should be Transparent. At the moment I have the Trigger in reverse so if the DataGrid SelectedItems.Count is 0 it is Transparent, if there is someway to have the default as transparent that would be nice.

Note: It will have multiple tabs and associated DataGrids.

How can I get the Style Trigger working correctly on the Ellipse?

enter image description here

<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>
Hank
  • 2,456
  • 3
  • 35
  • 83
  • You can bind the `Tag` property of each `TabItem` to the corresponding `DataGrid` and use this in your `Trigger`'s binding property path. – dymanoid Sep 05 '17 at 07:54

1 Answers1

1

Don't set the a local value for the Fill property of the Ellipse. Set the default value in a 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>

Local values take precedence over values set in styles: https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence

You may also have to bind to the Count property of a source collection that keeps track of the selected items rather than binding to the SelectedItems property of the DataGrid element. This is because a TabControl by default unloads the visual tree of the unselect tab when you switch tabs:

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

mm8
  • 163,881
  • 10
  • 57
  • 88
  • unfortunately the trigger still doesn't respond to when SelectedItems.Count = 0. The ellipse's fill is always blue, any ideas on what could be going on? I select and deselect an item and there is no change. – Hank Sep 05 '17 at 22:45
  • Creating a seperate collection of the SelectedItems worked, also I found that binding IsSelected on the DataGridRow to the row data object stops the actual selection from disappearing. – Hank Sep 05 '17 at 23:58