Listbox groupstyle绑定展开器IsExpanded属性

时间:2014-01-27 09:48:32

标签: c# wpf listbox

我有一个列表,其中包含在某些组中订购的用户。

这是我的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>

这是一个用户列表。每个用户都有一个带有某个groupId的userviewmodel,以查看他应该在哪个组中。

所以在我的mainviewmodel中包含了那些userviewmodel的列表,我设置了我的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();
        }
    }

这是我的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 个答案:

答案 0 :(得分:1)

一个简单的解决方案是添加

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

属性为UserViewModel

然后执行:

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

在你的模板上。

就像WPF一样简单有效