我有一个带有扩展器的模板化分组列表框的UserControl,并且只希望随时打开一个扩展器。我浏览了网站,但除了将IsExpanded绑定到IsSelected之外没有找到任何东西,这不是我想要的。
我试图在Expanded事件中放入一些代码,这些代码将遍历Expanders并关闭所有不是Expanded事件中传递的扩展器的代码。我似乎无法弄清楚如何得到它们。我已经尝试了ListBox.Items.Groups,但没有看到如何得到它们并尝试了ListBox.ItemContainerGenerator.ContainerFromItem(或索引),但没有任何回来。
由于
这是当前的标记:
<ListBox Name="ListBox">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Border BorderBrush="CadetBlue" BorderThickness="1">
<Expander BorderThickness="0,0,0,1" Expanded="Expander_Expanded"
Focusable="False"
IsExpanded="{Binding IsSelected,
RelativeSource={RelativeSource FindAncestor, AncestorType=
{x:Type ListBoxItem}}}" >
<Expander.Header>
<Grid>
<StackPanel Height="30" Orientation="Horizontal">
<TextBlock Foreground="Navy" FontWeight="Bold"
Text="{Binding Path=Name}" Margin="5,0,0,0"
MinWidth="200" Padding="3"
VerticalAlignment="Center" />
<TextBlock Foreground="Navy" FontWeight="Bold"
Text=" Setups: " VerticalAlignment="Center"
HorizontalAlignment="Right"/>
<TextBlock Foreground="Navy" FontWeight="Bold"
Text="{Binding Path=ItemCount}"
VerticalAlignment="Center"
HorizontalAlignment="Right" />
</StackPanel>
</Grid>
</Expander.Header>
<Expander.Content>
<Grid Background="white" >
<ItemsPresenter />
</Grid>
</Expander.Content>
<Expander.Style >
<Style TargetType="{x:Type Expander}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1">
<GradientStop Color="WhiteSmoke"
Offset="0.0" />
<GradientStop Color="Orange" Offset="1.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="false"
<Setter Property="Background">
<Setter.Value>
...
答案 0 :(得分:1)
在ListBoxItem的模板中,您可以使用共享同一组的RadioButtons,将IsChecked
绑定到IsSelected
的{{1}}并将其重新模板化为Expander,这样您就可以绑定{{ 1}} ListBoxItem
上的IsExpanded
。