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.
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...
I ended up using the Messenger classes from the MVVM Futures project to let the UserControl's ViewModel signal other ViewModels of the change.
This let's multiple listeners monitor for the same changes, without the need for extra Dependency Properties.
精彩评论