1

I have a search box which i am trying to to check if it is empty by using the "hasdata" and if empty return false else return true, but the DataTrigger Binding is not working. can someone point me in the right direction on what i am doing wrong.

code:

public bool hasdata
{
    get { if (searchBox.Text.Count() == 0) return false; else return true; }
}

xaml:

<telerik:RadWatermarkTextBox x:Name="searchBox"/>
<Image Source="SomeImage.png" >
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=hasdata}" Value="True">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=hasdata}" Value="False">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>
5
  • for this particular case, you should just binding the Visibility to your hasdata and set the binding Converter to BooleanToVisibilityConverter Commented Jul 11, 2013 at 16:31
  • 1
    What are you doing wrong? Practically everything, unfortunately. Your biggest problem is that you don't understand how databinding works. I'd strongly suggest you stop what you're doing and go search for "how does databinding work in WPF" here and on MSDN. You'll find many top links that describe the process. Commented Jul 11, 2013 at 16:33
  • @Bolu: That will, at most, be evaluated once. Commented Jul 11, 2013 at 16:34
  • @Will 100% yes, by that comment, I'm not suggesting that is a answer. just a side note. Commented Jul 11, 2013 at 16:36
  • 3
    just get rid of the has data property all together. Don't rly need it from what we see of your code. First set a Style Default Setter for Visibility as Visible. Next you only need one Trigger. Use a ElementName=searchBox, Path=Text binding and check if Value={x:Static sys:String.Empty} and switch the Visibility to Hidden. DP or INPC for a property that isnt even needed is just overkill. Oh and do follow @Will 's suggestion to read up on some basics Commented Jul 11, 2013 at 16:38

1 Answer 1

4

The UI has currently no way of being notified when hasdata is changed. You need to either implement the INotifyPropertyChanged interface or make hasdata a DependencyProperty.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.