0

我想让 TreeViewItem 的内容中包含空的 ListBox 看起来像没有任何内容的 TreeViewItem。例如,TreeViewItem“A”看起来像我希望的那样。当 TreeViewItem“B”中的 ListBox 为空时,我希望“B”看起来像“A”。

    <TreeView>
        <TreeViewItem x:Name="A" Header="Header" />
        <TreeViewItem x:Name="B" Header="Header with empty list">
            <ListBox x:Name="localContent" ItemsSource="{Binding listContent}">
                <!--ListBox.Style>
                    <Style TargetType="ListBox">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding listContent.Count}" Value="0">
                                <Setter Property="{Binding ElementName=localItem, Path=Background}">
                                    <Setter.Value>
                                        <Color R="0" G="0" B="0" /> 
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.Style-->
            </ListBox>
        </TreeViewItem>
    </TreeView>
4

3 回答 3

0

看起来你已经在用 count 做类似的事情了。

         <TreeViewItem x:Name="B" Header="Header with empty list">
            <ListBox x:Name="localContent" ItemsSource="{Binding listContent}">
                <ListBox.Style>
                    <Style TargetType="ListBox">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding listContent.Count}" Value="0">
                                <Setter Property="Visiblity" Value="Hidden" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.Style>
            </ListBox>
        </TreeViewItem>
于 2012-09-06T20:28:33.407 回答
0

您可以将Visibility您的 ListBox 设置Collapsed为使其不会出现在您的TreeView

于 2012-09-06T20:25:43.367 回答
0

使用以下触发器实现了所需的行为(为任何控件添加它):

<Style.Triggers>
            <DataTrigger Binding="{Binding Count, Converter={StaticResource int2bool}}" Value="False">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Expander Visibility="Hidden" />
                                <TextBlock Text="list is empty" />
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Count, Converter={StaticResource int2bool}}" Value="True"  >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander Header="{Binding}">
                                <Expander.Content>
                                    <ListBox ItemsSource="{Binding}" />
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
于 2012-09-06T23:38:35.723 回答