Question

Below is a fine piece of artwork that represents a WPF form with a listbox on the left and a content control on the right. I would like to set it so if the list box is empty, then the content control is invisible. What property/event should I hook to?

----- -----
| a | | c |
| b | |   |
----- -----
Was it helpful?

Solution

You should create a Style for the the ContentControl, and use a Trigger to determine when the List has 0 items, like so:

<ListBox x:Name="uiList">...</ListBox>
<ContentControl>
        <ContentControl.Content>
            <TextBox Text="List has items." />
        </ContentControl.Content>
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=uiList, Path=Items.Count}"
                                 Value="0">
                        <Setter Property="Visibility"
                                Value="Collapsed" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top