简体   繁体   English

列表框groupstyle绑定扩展器IsExpanded属性

[英]Listbox groupstyle bind expander IsExpanded property

I have a list with users that are ordered in some groups. 我有一个列表,其中列出了按某些组排序的用户。

This is my ListBox xaml : 这是我的ListBox 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>

This is a list of users. 这是用户列表。 Each user has a userviewmodel with a certain groupId to see in which group he should be. 每个用户都有一个带有特定groupId的userviewmodel,以查看他应该属于哪个组。

So in my mainviewmodel which contains a list of those userviewmodels I set my groupdescriptions 因此,在包含这些userviewmodels列表的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();
        }
    }

And this is my GroupDescriptionConverter 这是我的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();
    }
}

I want to bind the IsExpanded property on my expander on the ExpandGroup property of my group object. 我想将我的扩展器上的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()
    {
    }
}

A simple solution for this is to add a 一个简单的解决方案是添加一个

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

property to your UserViewModel 属性到您的UserViewModel

and then do: 然后执行:

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

on your template. 在您的模板上。

Simple and effective just like WPF should be 就像WPF一样简单有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM