【发布时间】:2011-06-04 20:14:07
【问题描述】:
我有一个指定 ItemContainerStyle 的 ListBox。 ItemContainerStyle 使用 Triggers 来设置 ContentTemplate。
我想在应用模板时在 ContentTemplate 中设置 ContentControl(名为“ExpanderContent”)的可见性。如果可能,我更喜欢使用 ListBoxItem 上设置的附加属性的值。
也许这有助于使问题更清楚,看看我尝试使用应用于 ContentControl 的样式(名为“ExpanderContent”)的一个示例。我意识到在下面的代码中没有在 ContentControl 上设置样式。我已设置并看到它已应用,并看到解析附加属性没有错误。
<ListBox ItemContainerStyle="{StaticResource lbcStyle}"/>
<Style TargetType="ListBoxItem" x:Key="lbcStyle">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource editable}"/>
</Trigger>
</Style.Triggers>
<Setter Property="ContentTemplate" Value="{StaticResource nonEditable}"/>
</Style>
<DataTemplate x:Key="nonEditable">
<Grid Width="Auto" Height="Auto">
...
<ContentControl Name="ExpanderContent" Visibility="Collapsed" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="12"></ContentControl>
</Grid>
</DataTemplate>
<DataTemplate x:Key="editable">
<Grid x:Name="grdEditable" Width="Auto" Height="Auto">
...
<ContentControl Name="ExpanderContent" Visibility="Collapsed" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="12"></ContentControl>
</Grid>
</DataTemplate>
<Style x:Key="editorContentControl" TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding (local:AttachedProperties.IsExpanded),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding (local:AttachedProperties.IsExpanded),RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Visibility" Value="Visible"/>
</Style>
这个废话的真实世界应用程序是我有一个列表框,列表框中的项目具有在所选时更改的模板。模板有一个扩展器,即使未选择该项目,我也想让扩展器保持扩展状态。就像现在一样,每当我更改列表框选择时,内容模板都会以其原始状态重新应用,即折叠 - 所以我一次不能展开多个。
【问题讨论】:
标签: wpf datatemplate visibility