Search code examples
wpfxamlwpf-controlsdatatrigger

ControlTemplate DataTrigger not Updating when Bound to Another Control


I've been creating a Custom Control that includes a Group Panel on top of a Datagrid.

I have a watermark textblock that should collapse once I add children to the PART_GroupPanel via Code Behind.

The following code is part of a ControlTemplate that I will be using multiple times across the application.

<!--Watermark Textblock-->
<TextBlock Text="Drag &amp; Drop Column to Group." Foreground="{DynamicResource FirstThemeBrush}" FontFamily="Calibri" Margin="8,0,0,0" VerticalAlignment="Center">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=PART_GroupPanel, Path=Children.Count}" Value="0">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
<!--Panel-->
<StackPanel x:Name="PART_GroupPanel" Margin="0,1,0,2" Orientation="Horizontal">
    <StackPanel.Resources>
        <Style TargetType="Button" BasedOn="{StaticResource RoundedButtonStyle}">
            <Setter Property="Padding" Value="15,5"/>
            <Setter Property="Margin" Value="0,0,0,0"/>
        </Style>
        <Style TargetType="Path">
            <Setter Property="Width" Value="5"/>
            <Setter Property="Height" Value="8"/>
            <Setter Property="Margin" Value="5,0"/>
            <Setter Property="Fill" Value="{DynamicResource FirstThemeBrush}"/>
            <Setter Property="Stretch" Value="Fill"/>
            <Setter Property="Data" Value="M 0 6 L 2 0 L 25 25 L 2 50 L 0 42 L 16 25 L 0 6 Z"/>
        </Style>
    </StackPanel.Resources>
</StackPanel>

The DataTrigger Doesn't Update and Collapse the Textblock Once Items are Added via Code Behind.

Any Help is appreciated.


Solution

  • To solve this, use an ItemsControl instead. It will essentially function like a StackPanel as it uses a StackPanel for its layout.

    <TextBlock
        Margin="8,0,0,0"
        VerticalAlignment="Center"
        FontFamily="Calibri"
        Foreground="{DynamicResource FirstThemeBrush}"
        Text="Drag &amp; Drop Column to Group.">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Visibility" Value="Collapsed" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=PART_GroupPanel, Path=Items.Count}" Value="0">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    
    <ItemsControl x:Name="PART_GroupPanel"
        Margin="0,1,0,2">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>