2014-01-27 81 views
0

我有一個列表,其中有一些用戶在某些組中排序。Listbox groupstyle綁定擴展器IsExpanded屬性

這是我的列表框XAML:

  <ListBox x:Name="UserContainer" 
        ItemsSource="{Binding allUserViewModel.UserView}" 
        Background="Transparent" 
        HorizontalContentAlignment="Stretch" 
        ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
        ScrollViewer.VerticalScrollBarVisibility="Auto" 
        BorderThickness="0" Margin="0,0,0,7" Padding="0" 
        ItemContainerStyle="{DynamicResource ListBoxItemStyle}" 
        Visibility="{Binding allUserViewModel.UserView.Count, Converter={StaticResource ShowIfHasUsersConverter}}" 
        MouseEnter="UserContainer_OnMouseEnter" MouseLeave="UserContainer_OnMouseLeave"> 

       <ListBox.Style> 
        <Style> 
         <Style.Triggers> 
          <Trigger Property="ListBox.IsGrouping" Value="true"> 
           <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </ListBox.Style> 

       <ListBox.GroupStyle> 
        <GroupStyle> 
         <GroupStyle.ContainerStyle> 
          <Style TargetType="{x:Type GroupItem}"> 
           <Setter Property="Template"> 
            <Setter.Value> 
             <ControlTemplate TargetType="{x:Type GroupItem}"> 
              <Expander IsExpanded="{Binding Path=Name.IsExpanded}" Style="{StaticResource EditedMetroExpander}" Padding="0,3,0,3"> 
               <Expander.Header> 
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> 
                 <TextBlock Text="{Binding Name}" Foreground="Silver" 
                    FontSize="18" FontFamily="Segoe UI Light" VerticalAlignment="Center" /> 
                </StackPanel> 
               </Expander.Header> 

               <ItemsPresenter /> 
              </Expander> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </GroupStyle.ContainerStyle> 
        </GroupStyle> 
       </ListBox.GroupStyle> 

       <ListBox.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> 
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
       </ListBox.Resources> 

       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <view:UserControlButton x:Name="UserControlButton" /> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

這是用戶的列表。每個用戶都有一個具有特定groupId的userviewmodel,以查看他應該在哪個組中。

我mainviewmodel

所以其中包含設置我groupdescriptions

public AllUserViewModel() 
    { 
     UserList = new ObservableCollection<UserViewModel>(); 
     OnlineContacts = new List<UserViewModel>(); 
     LocalContacts = new List<UserViewModel>(); 

     UserView = (CollectionView)CollectionViewSource.GetDefaultView(UserList); 

     // Filter 
     if (UserView.CanFilter) 
      UserView.Filter = OnFilterUsers; 

     // Group 
     if (UserView.CanGroup && UserView.GroupDescriptions != null) 
      UserView.GroupDescriptions.Add(new PropertyGroupDescription(UserViewModel.GroupIdPropertyKey, new GroupDescriptionConverter())); 

     // Sorting 
     if (UserView.CanSort) 
     { 
      // Custom sort only available with ListCollectionView 
      ListCollectionView userListView = (ListCollectionView)CollectionViewSource.GetDefaultView(UserList); 
      userListView.CustomSort = new CustomUserComparer(); 
     } 
    } 

那些userviewmodels的列表,這是我的GroupDescriptionConverter

public class GroupDescriptionConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var groupId = (int)value; 
     var group = GroupController.Instance.GetGroup(groupId); 

     return group.Name; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

我想將IsExpanded屬性綁定我在膨脹我的組對象的ExpandGroup屬性。

public class Group 
{ 

    public int Id { get; set; } 
    public int OrderId { get; set; } 
    public string Name { get; set; } 
    public bool ExpandGroup { get; set; } 
    public bool NewLine { get; set; } 

    public Group() 
    { 
    } 
} 

回答

1

這樣做的一個簡單的解決方案是將

private bool _isExpanded = true; 
public bool IsExpanded 
{ 
    get { return _isExpanded; } 
    set { _isExpanded = value; } 
} 

屬性添加到您的UserViewModel

然後執行:

<Expander IsExpanded="{Binding Items[0].IsExpanded}"> 

您的模板。

簡單而有效就像WPF應該是