从ListView中打开所选项目的扩展器 [英] Open an Expander of a selected item from a ListView

查看:74
本文介绍了从ListView中打开所选项目的扩展器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过代码选择ListView的项.这些项目被分组并包装在扩展器中.因此,如果选择了一个包含的项目,也应该打开一个扩展器.
现在,我很难通过代码或触发器来做到这一点.如何做到这一点?

这是ListView的代码:


I need to select an item of a ListView by code. The items are grouped and wrapped in Expanders. Therefore an Expander should be opened as well if a contained item is selected.
Now, I have difficulties to do that either by code or by triggers. How can be done that?

This is the code of the ListView:


<身体>
< UserControl.Resources >
< local:NAV x: =
< CollectionViewSource x: = =
<UserControl.Resources> 
 
    <local:NAV x:Key="myNavList" /> 
 
    <CollectionViewSource x:Key='src2' Source="{Binding Source={StaticResource myNavList}, Path=NavGetList}">  
        <CollectionViewSource.GroupDescriptions> 
            <PropertyGroupDescription PropertyName="ACategory" /> 
        </CollectionViewSource.GroupDescriptions> 
    </CollectionViewSource> 
 
 
 
    <Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">  
        <Setter Property="Height" Value="0"/>  
    </Style> 
 
</UserControl.Resources> 
 
 
 
 
<Grid Name="GridN">  
 
    <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" MinHeight="50" /> 
        <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
 
    <ListView Name="ListProfile"     
                  Grid.Row="2"   
                  ItemsSource="{Binding Source={StaticResource src2}}"   
                  SelectedValuePath="{Binding Path=Men_id}" 
                  BorderThickness="0"   
                  ScrollViewer.VerticalScrollBarVisibility="Visible"   
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                  SelectionMode="Single" > 
        <ListView.GroupStyle> 
            <GroupStyle> 
                <GroupStyle.ContainerStyle> 
                    <Style TargetType="{x:Type GroupItem}">  
                        <Setter Property="Margin" Value="0,0,0,5"/>  
                        <Setter Property="Template">  
                            <Setter.Value> 
                                <ControlTemplate TargetType="{x:Type GroupItem}">  
 
                                    <Expander x:Name="Expo" IsExpanded="False" > 
                                        <Expander.Header> 
                                            <DockPanel Style="{StaticResource ExHead}" > 
                                                <TextBlock Text="{Binding Path=Name}"   
                                                               Margin="5,0,0,0" Width="50" DockPanel.Dock="Left"/>  
                                                <TextBlock Text="{Binding Path=ItemCount}" /> 
                                            </DockPanel> 
                                        </Expander.Header> 
                                        <Expander.Content> 
                                            <ItemsPresenter /> 
                                        </Expander.Content> 
                                    </Expander> 
 
                                </ControlTemplate> 
                            </Setter.Value> 
                        </Setter> 
                    </Style> 
                </GroupStyle.ContainerStyle> 
            </GroupStyle> 
        </ListView.GroupStyle> 
 
        <ListView.View> 
            <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}" > 
                <GridViewColumn DisplayMemberBinding="{Binding Path=AStatus}" Width="20" /> 
                <GridViewColumn DisplayMemberBinding="{Binding Path=AValue}" /> 
            </GridView> 
        </ListView.View> 
 
    </ListView> 
 
</Grid> 
 
</UserControl> 
 

推荐答案

解决方案.这很丑陋,有点慢.无法访问选定项目中的组吗?

I have found a solution. It is quite ugly and a bit slow. Is there no other possibilities to access groups from a selected item?

<身体>
SubFindPerson(ByVal ID作为向导)
尝试
ListProfile.UnselectAll()
对于每个i作为TNav在ListProfile.Items中
如果CType(i.AId,Guid)= ID,则
ListProfile.SelectedItem = i 字体>
扩展组(i.ACategory)
退出
结束如果
下一个
异常捕获
MessageBox.Show(ex.Message)
结束尝试
结束Sub
私人SubExpandGroup(ByVal Gruppe As String)
昏暗的As Integer
对于ListProfile.Items.Groups中的每个h
h.Name = 然后
退出
结束如果
n = + 1
下一步
昏暗的人作为DependencyObject
pa == ListProfile .ItemContainerGenerator.ContainerFromItem(ListProfile.Items.Groups(n))
Dim aaa作为 扩展器 = VisualTreeHelper .GetChild(pa,́0)
aaa.IsExpanded = 真实
ListProfile.Items.Dispatcher.BeginInvoke(Windows.Threading.DispatcherPriority.Background,_
新建SetSelectedItemDelegate(AddressOfSetSelectedItem))
结束Sub
PublicDelegate Sub SetSelectedItemDelegate()
Public Sub SubSetSelectedItem()
ListProfile.ScrollIntoView(ListProfile.SelectedItem)
结束Sub
    Sub FindPerson(ByVal ID As Guid)  
        Try  
            ListProfile.UnselectAll()  
            For Each i As TNav In ListProfile.Items  
                If CType(i.AId, Guid) = ID Then  
                    ListProfile.SelectedItem = i  
                    ExpandGroup(i.ACategory)  
                    Exit For  
                End If  
            Next  
        Catch ex As Exception  
            MessageBox.Show(ex.Message)  
        End Try  
    End Sub  
 
    Private Sub ExpandGroup(ByVal Gruppe As String)  
        Dim n As Integer  
        For Each h In ListProfile.Items.Groups  
            If h.Name = Gruppe Then  
                Exit For  
            End If  
            n += 1  
        Next  
        Dim pa As DependencyObject  
        pa = ListProfile.ItemContainerGenerator.ContainerFromItem(ListProfile.Items.Groups(n))  
        Dim aaa As Expander = VisualTreeHelper.GetChild(pa, 0)  
        aaa.IsExpanded = True 
        ListProfile.Items.Dispatcher.BeginInvoke(Windows.Threading.DispatcherPriority.Background, _  
                                 New SetSelectedItemDelegate(AddressOf SetSelectedItem))  
    End Sub  
 
    Public Delegate Sub SetSelectedItemDelegate()  
 
    Public Sub SetSelectedItem()  
        ListProfile.ScrollIntoView(ListProfile.SelectedItem)  
    End Sub  
 
 
 


这篇关于从ListView中打开所选项目的扩展器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆