views:

867

answers:

1

I have xaml that lookes like this

<ListBox>
<ListBox.ItemTemplate>
 <DataTemplate>
  <StackPanel>
   <TextBlock Text="{Binding Name}" />
   <StackPanel Orientation="Vertical" x:Name="contentPanel" >
   Content goes here...
   </StackPanel>
  </StackPanel>
 </DataTemplate>
</ListBox.ItemTemplate>

The listbox binds to an object with a bool property called ShowContent. How do I get silverlight to hide the contentPanel if the object with ShowContent is false?

+2  A: 

Write a BoolToVisibility IValueConveter and use it to bind to the Visibility property of your contentPanel

<StackPanel Visibility="{Binding YourBoolProperty, Converter={StaticResource boolToVisibilityResourceRef ..../>

You can find a BoolToVisibility pretty easy anywhere.

Check IValueConveter if you are new to that. http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

Jobi Joy
There is a BooleanToVisibilityConverter which is part of the framework.
gcores
I have tried that and it doen't work. It will probably work in wpf but not in silverlight.
Emil C
:) It should work in Silverlight - Here is a quick search result http://www.jeff.wilcox.name/2008/07/visibility-type-converter/
Jobi Joy
Tried it again and it worked.
Emil C