WPF - 只希望在分组列表框中一次打开一个扩展器

发布于 2024-09-03 05:40:19 字数 3431 浏览 5 评论 0原文

我有一个带有带有扩展器的模板化分组列表框的用户控件,并且只希望随时打开一个扩展器。我浏览过该网站,但除了将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-09-10 05:40:19

在 ListBoxItem 的模板中,您可以使用共享同一组的 RadioButton,将其 IsChecked 绑定到 ListBoxItemIsSelected 并将其重新模板化为 Expander,这样您可以将 IsExpanded 绑定到 TemplatedParent 上的 IsChecked

In the ListBoxItem's template you can use RadioButtons that share the same group, bind their IsChecked to IsSelected of the ListBoxItem and retemplate it as Expander, so you can bind the IsExpanded to IsChecked on the TemplatedParent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文