views:

157

answers:

1

I have a UserControl that contains a listbox.

On the parent window, I have this UserControl and a button.

Ideally I'd like to use the ChangePropertyAction behavior on the parents button, and tie it to the UserControl's listbox count.

The idea being that if there are no entries in the listbox inside the usercontrol, the button on the parent window is hidden. The listbox is bound to an observablecollection.

Do I create a DependencyProperty to do this? I'm not sure how to bind the listbox's count to this property though.

Thanks so much for any insight into the right way to do this.

A: 

You can use a ElementName Binding to reach the ListBox state from the Button. You then want to use a BooleanToVisibilityConverter to do the magic.

Like so:

<Window x:Class="NestedTreeTest.Window1"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="Window1" Height="200" Width="300">

 <Window.Resources>
  <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
 </Window.Resources>

 <StackPanel>
  <Button Visibility="{Binding ElementName=myList, Path=HasItems, Converter={StaticResource boolToVisibilityConverter}}">
    Text
  </Button>
  <ListBox x:Name="myList">
   <!--<ListBoxItem>Item A</ListBoxItem>-->
  </ListBox>
 </StackPanel>
</Window>

comment out, or uncomment the ListBoxItems to see it working...

Simeon Pilgrim
Thank you for the help. The problem though is the listbox is inside a UserControl. So on Window1, It's more:<StackPanel> <Button Visibility="{Binding ElementName=myList, Path=HasItems, Converter={StaticResource boolToVisibilityConverter}}"> Text </Button> <Controls:MyControl /> </StackPanel>And I need to bind to a listbox in MyControl. I'm not sure how to expose that from the UserControl is the issue.
mattjf