【问题标题】:How to select first item in expanded group (listview)如何选择扩展组中的第一项(列表视图)
【发布时间】:2012-02-07 09:00:30
【问题描述】:

谁能帮我解决这个问题(c# wpf):

我有一个ListView 和这个Style 用于我的Expander(每个组):

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander IsExpanded="False">
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Path=Name}" />
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>

当用户展开Expander时,我想选择展开组中的第一项。

我这样添加了我的组(CustomerOrderList = ListView):

CustomerOrderList.ItemsSource = OrderDetails.DefaultView;
CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(CustomerOrderList.ItemsSource);
PropertyGroupDescription pgd = new PropertyGroupDescription("OrderInfo");
cv.GroupDescriptions.Add(pgd);

这可能吗?

谢谢, 森内

【问题讨论】:

    标签: c# wpf xaml listview expander


    【解决方案1】:

    是的,这是可能的。

    包含 Linq 命名空间

     using System.Linq;
    

    处理扩展器的Expanded 事件

      <Expander Expanded="GroupExpander_Expanded" IsExpanded="False" ... /> 
    

    代码背后...

       private void GroupExpander_Expanded(object sender, RoutedEventArgs e)
       {
            var expander = sender as Expander;
            //Extract the group 
            var groupItem = expander.DataContext as CollectionViewGroup;
            //Set the first item from the group to ListBox's Selected Item property.
            CustomerOrderList.SelectedItem = groupItem.Items.First();
       }
    

    如果您使用的是 MVVM,则使用 Attached Property 行为将此功能封装到其中。

    希望这会有所帮助,

    【讨论】:

    • 哇,太好了。这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2010-12-14
    • 2014-12-06
    相关资源
    最近更新 更多