I have a treeView and a stackpanel with message that “There is no items”. So, I want to hide panel if treeView’s items is not empty.
Here is my XAML example:
<TreeView Name="treeDocs" Grid.ColumnSpan="2"/>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center"
Margin="51,20,51,0" Name="stkNoDocs"
Visibility="{Binding ElementName=treeDocs, Path=Items,
Converter={StaticResource ResourceKey=ItemsToVisibilityConverter}}">
And here is my Converter’s Convert method:
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return ((ItemCollection)value).Count == 0 ? Visibility.Visible : Visibility.Collapsed;
}
I’ve used the Style property like this:
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=treeDocs, Path=Items.Count}" Value="0">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
- but still same result.
And in design mode everything works perfect! But in fact panel always visible… What’s the problem?
Thank you!
The instance stored in
Itemsnever changes (its collection contents do though), hence the binding does not get updated, bind toItems.Countand change the converter accordingly or use aStylewith aDataTriggerwhich would be more appropriate than a converter.Something like this:
(Default visibility needs to be set in a setter because of dependency property precedence)