WPF - 只希望在分组列表框中一次打开一个扩展器
我有一个带有带有扩展器的模板化分组列表框的用户控件,并且只希望随时打开一个扩展器。我浏览过该网站,但除了将 IsExpanded 绑定到 IsSelected 之外没有找到任何内容,这并不是我想要的。
我试图在 Expanded 事件中放入一些代码,这些代码将循环 Expanders 并关闭所有不是 Expanded 事件中传递的 Expander 的代码。我似乎不知道如何获得它们。我尝试过 ListBox.Items.Groups 但不知道如何获取它们并尝试过 ListBox.ItemContainerGenerator.ContainerFromItem (或 Index)但没有任何返回。
谢谢
这是当前的标记:
<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>
...
I have a UserControl with a templated grouped listbox with expanders and only want one expander open at any time. I have browsed through the site but haven't found anything except binding the IsExpanded to IsSelected which isn't quite what I want.
I am trying to put some code in the Expanded event that would loop through Expanders and close all the ones that aren't the expander passed in the Expanded event. I can't seem to figure out how to get at them. I've tried ListBox.Items.Groups but didn't see how to get at them and tried ListBox.ItemContainerGenerator.ContainerFromItem (or Index) but nothing came back.
Thanks
Here is the current markup:
<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>
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ListBoxItem 的模板中,您可以使用共享同一组的 RadioButton,将其
IsChecked
绑定到ListBoxItem
的IsSelected
并将其重新模板化为 Expander,这样您可以将IsExpanded
绑定到TemplatedParent
上的IsChecked
。In the ListBoxItem's template you can use RadioButtons that share the same group, bind their
IsChecked
toIsSelected
of theListBoxItem
and retemplate it as Expander, so you can bind theIsExpanded
toIsChecked
on theTemplatedParent
.