![](/img/trans.png)
[英]How to find the child control's IsEnabled property in ListBoxItem for Trigger
[英]Is there a way to hidden ListBoxItem by Visibility property of it's child
有一個非常簡單的 WPF UI,沒有背后的代碼,xaml 的部分如下:
<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>
...
我希望當 StackPanel 折疊時,ListBoxItem 的 BorderThickness 為 0。因為當 StackPanel 折疊時,我們可以看到項目的邊框線。 怎么做?
是的,有可能,只需將 ItemContainerStyle 中的 Visibility 綁定到內容可見性屬性,例如:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.