views:

116

answers:

1

Hi All, I need to display a button depending on the value in the database. For Eg. If the value of the object is empty I need to display a button that says Create Data. And if the object does have a value, I need to display 2 buttons that say Update Data and Edit data. I tried to use 2 converters for this. The first one displays Create button correctly, but the Update and Edit buttons get displayed always whether the object has value or not.

+1  A: 

It looks like you have your data binding wrong. Perhaps name which you bind to is always empty. What is your DataContext?

There seems to be nothing wrong with the converters.

Vlad
Nope I checked that out and it is not always empty. Like I have 4 records out of which 1 has data in name field and others do not. It does display it correctly if I bind it to content property of the button. Is there some other way of attaining the same thing? Like I said with the above code, the Create button gets displayed correctly but the Update and the Edit do not. Is it possible to just use one converter and attain the above functionality instead of using 2 like I have used.
developer
I got what was happening, but I don't know the solution to that. If a record was present but with Name field empty, it displays everything correctly. But if a record was not present at all in the database, it displayed the buttons incorrectly. Now is there a way to check whether a record is present in the database, if not display the buttons?
developer
You can bind to the object itself, not to the name: <Button Content="Edit" Visibility="{Binding Converter={StaticResource EditVisibilityConverter}}"/> In the converter you should get the following cases: if (value == null) { /* not in the database */ } else { Foo foo = (Foo)value; string name = foo.name; return string.IsNullOrEmpty(name) ? Visibility.Visible : Visibility.Collapsed; }Hope that helps.
Vlad
Thank you so much Vlad. It does help a lot. Thanks againg for all your help.
developer
It's okay, you are welcome anyway! You can mark the answer as useful, if you would like to.
Vlad
I did that. Thanks a lot.
developer