TextBlock trigger instead of using a converter

I want to show the number on the screen. If this number is 0, I do not want it to be displayed at all.

<TextBlock Text="{Binding Path=Class.Count}" FontSize="20" FontWeight="Bold">
    <TextBlock.Triggers>
        <DataTrigger Binding="{Binding Path=Class.Count}" Value="0">
            <Setter Property="TextBlock.Text" Value=""/>
        </DataTrigger>
    </TextBlock.Triggers>
</TextBlock>

I tried to execute the above part of the code after a normal trigger could not solve my problem. I do not want to write a converter to act on one specific number. Is there a way to create a trigger that will hide the number if it is 0?

EDIT: When I try to use a regular data trigger or trigger, I get a xaml syntax error telling me that I need to use an event trigger.

I tried setting the value in setter to a number to make sure that having an empty value did not cause a problem.

+5
1

, Text.

/ , String.Empty.

.

<TextBox>
    <TextBox.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Name.Length}" Value="0">
                    <Setter Property="UIElement.Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Name.Length}" Value="0">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
  • DataTriggers.
  • xaml , . UIElement. , , .
+8

All Articles