简体   繁体   中英

Is there a way to hidden ListBoxItem by Visibility property of it's child

there is a very simple WPF UI,no behind code, the part of xaml is below:

<ListBox Grid.Row="0" x:Name="lst" FocusVisualStyle="{x:Null}" BorderThickness="0">
            <ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
                    <Setter Property="Foreground" Value="{StaticResource YDTTextColor}" />
                    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                    <Setter Property="BorderThickness" Value="1" />
                    **<Setter Property="Visibility" Value="{Binding }" />** 
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border x:Name="Bd"
                                BorderBrush="{StaticResource YDTBorderColor}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                Margin="0,6" Visibility="Collapsed"
                                CornerRadius="4" SnapsToDevicePixels="True">
                                    <ContentPresenter />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter Property="BorderBrush"
                                        TargetName="Bd"
                                        Value="{StaticResource YDTMainBackColor}"/>
                                        <Setter Property="Foreground" Value="Red" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
</ListBox.ItemContainerStyle>
<ListBox.Items>
<StackPanel Orientation="Horizontal"  Tag="NegotiateSale" Height="Auto" 
                                Visibility="{Binding Source={x:Static bo:SysParameter.MDPOS0075}, Converter={StaticResource visibilityConverter}}">
                        <Image Source="../../Images/NegotiateSale.png" Height="32" Margin="30,0,14,0"/>
                        <TextBlock Text="someText"/>
                </StackPanel>
...

i hope when the StackPanel Collapsed,the ListBoxItem's BorderThickness is 0. because when the StackPanel Collapsed,the border line of the item we can see it. how to do it?

Yes, it is possible, just bind Visibility in ItemContainerStyle to the content visibility property, eg.:

 <ListBox
        x:Name="lst"
        Grid.Row="0"
        BorderThickness="0"
        FocusVisualStyle="{x:Null}"
        >
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="Visibility" Value="{Binding Visibility, Mode=OneWay}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border
                                BorderBrush="Green"
                                BorderThickness="2"
                                >
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.Items>
            <StackPanel
                Width="50"
                Height="50"
                Background="Red"
                Visibility="Collapsed"
                >
                <Image
                    Height="32"
                    />
            </StackPanel>
            <StackPanel
                Width="50"
                Height="50"
                Background="Yellow"
                Visibility="Visible"
                >
                <Image
                    Height="32"
                    />
            </StackPanel>
        </ListBox.Items>
    </ListBox>

As you can see in the attached picture the yellow square is visible but red is not在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM