2012-02-07 146 views
2

谁能帮我解决这个问题(C#WPF):如何选择扩张组(列表视图)第一项

我有这个Style一个ListViewExpander(每个组):

<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); 

这可能吗?

感谢, Senne

回答

3

是的,它是可能的。

包括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然后使用ATTAC hed Property将此功能封装到其中的行为。

希望这会有所帮助,

+0

哇,太好了。这正是我正在寻找的。 – Senne 2012-02-07 10:36:36

相关问题