Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Visibility to Text.Length

Tags:

wpf

xaml

I implemented a small visual indicator (just a textblock with a border), that should be hidden if there is no text to be shown at the moment. The text is bound to the Indicator property, the data context seems to be set correctly.

What I got so far is this (indicator text appears, hide/show doesn't work):

<Border>
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Setter Property="Visibility" Value="Visible"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Indicator.Length}" Value="0">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <TextBlock Text="{Binding Indicator}" />
</Border>

My problem is that the element is not hidden if the text length is zero.

Do you spot my mistake?

Indicator is part of the corresponding viewmodel:

public string Indicator
{ get; set;}

UPDATE

It works if I change the property above to this:

public const string IndicatorPropertyName = "Indicator";
private string _indicator = "";
public string Indicator
{
    get
    { return _indicator;}

    set
    {
        if (_indicator == value) { return;}

        RaisePropertyChanged(IndicatorPropertyName);
    }
}

Why does it only work, if I raise PropertyChanged event?

like image 205
nabulke Avatar asked Jul 29 '26 03:07

nabulke


1 Answers

I think as the name implies a Trigger only executes or checks its state when an event occurs. In case of a DataTrigger it is the PropertyChanged-Event of the interface INotifyPropertyChanged.

Wihout raising the Event the DataTrigger doesn´t know that he has to check the binding and if the value meets the trigger condition.

like image 182
Jehof Avatar answered Aug 01 '26 06:08

Jehof



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!