0

使用 CollectionViewSource 可以轻松地对 ListBoxItem 进行分组。但每组Header,只有一个“Name”数据。如何将更多数据放入 Header?

像图片一样。

  1. 我想在标题中使用自定义控件,并将视图模型绑定到它。但我不知道如何从分组父级获取视图模型。
  2. 并且,<ItemsPresenter/>groupItem 的部分。如果我想将某些东西绑定到它,我该怎么办?

并且,我的代码:

<ListBox ItemsSource="{Binding Source={StaticResource XamlBookMarks}}"
         ItemTemplate="{StaticResource TemplateBookMark}">
    <ListBox.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource StyleBookMarkGroup}" />
    </ListBox.GroupStyle>
</ListBox>

 <Style x:Key="StyleWorkSiteGroup" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <StackPanel>
                        <ContentControl Content="{Binding}" ContentTemplate="{StaticResource TemplateHeader}" />
                        <!--here, i want binding something to ItemsPresenter-->
                        <ItemsPresenter/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

     <DataTemplate x:Key="TemplateHeader" DataType="{x:Type GroupItem}">
        <DockPanel>
            <DockPanel.Resources>
                <converters:HeaderGetter x:Key="HeaderConverter" />
            </DockPanel.Resources>
            <ContentControl DockPanel.Dock="Top" Content="{Binding Name, Converter={StaticResource HeaderConverter}, ConverterParameter={StaticResource XamlHeaders}}">
                <ContentControl.Resources>
                    <DataTemplate DataType="{x:Type local:GroupOneHeaderViewModel}">
                        <views:GroupOneHeaderView />
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type local:GroupTwoHeaderViewModel}">
                        <views:GroupTwoHeaderView />
                    </DataTemplate>
                </ContentControl.Resources>
            </ContentControl>
        </DockPanel>
    </DataTemplate>

在此处输入图像描述

4

1 回答 1

0

我做了一个包装类,但是感觉很丑。有没有其他方法可以做到这一点?

1,从

http://www.11011.net/wpf-transitions

2和

public class GroupItemsSelector : FrameworkElement
{
    public static readonly DependencyProperty SourceProperty =
        DependencyProperty.Register("Source",
                                    typeof (CollectionViewSource),
                                    typeof (GroupItemsSelector),
                                    new PropertyMetadata(OnSourceChangedCallback));

    public static readonly DependencyProperty IdProperty =
        DependencyProperty.Register("Id",
                                    typeof (string),
                                    typeof (GroupItemsSelector),
                                    new PropertyMetadata(OnSourceChangedCallback));

    public static readonly DependencyProperty CurrentProperty =
        DependencyProperty.Register("Current",
                                    typeof (object),
                                    typeof (GroupItemsSelector),
                                    new PropertyMetadata(OnCurrentItemChanged));

    public static readonly DependencyProperty WatchingProperty =
        DependencyProperty.Register("Watching",
                                    typeof (string),
                                    typeof (GroupItemsSelector),
                                    new PropertyMetadata(OnCurrentItemChanged));

    public static readonly DependencyProperty WatchingValueProperty =
        DependencyProperty.Register("WatchingValue",
                                    typeof (object),
                                    typeof (GroupItemsSelector));

    private readonly Transition _transition = new Transition();

    public GroupItemsSelector()
    {
        var dpd = DependencyPropertyDescriptor.FromProperty(Transition.StateProperty, typeof (Transition));
        if (dpd == null) return;
        dpd.AddValueChanged(_transition, delegate { WatchingValue = _transition.Source; });
    }

    public object WatchingValue
    {
        get { return GetValue(WatchingValueProperty); }
        private set { SetValue(WatchingValueProperty, value); }
    }

    public string Watching
    {
        get { return (string) GetValue(WatchingProperty); }
        set { SetValue(WatchingProperty, value); }
    }

    public CollectionViewSource Source
    {
        get { return (CollectionViewSource) GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }

    public string Id
    {
        get { return (string) GetValue(IdProperty); }
        set { SetValue(IdProperty, value); }
    }

    public object Current
    {
        get { return GetValue(CurrentProperty); }
        private set { SetValue(CurrentProperty, value); }
    }

    private static void OnCurrentItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = d as GroupItemsSelector;
        if (selector == null) return;
        selector.SetWatchingProperty();
    }

    private static void OnSourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = d as GroupItemsSelector;
        if (selector == null) return;
        selector.SetCurrent();
    }

    private void SetCurrent()
    {
        IDictionary dictionary = null;
        bool hasValue = false;
        try
        {
            if (Source == null || string.IsNullOrEmpty(Id)) return;
            dictionary = Source.Source as IDictionary;
            if (dictionary == null) return;
            hasValue = dictionary.Contains(Id);
        }
        finally
        {
            Current = (hasValue) ? dictionary[Id] : null;
        }
    }

    private void SetWatchingProperty()
    {
        _transition.DataContext = Current as INotifyPropertyChanged;
        if (string.IsNullOrEmpty(Watching))
        {
            BindingOperations.ClearBinding(_transition, Transition.SourceProperty);
        }
        else
        {
            _transition.SetBinding(Transition.SourceProperty, new Binding(Watching));
        }
    }
}

3. 和样品

<Style x:Key="StyleGroupItem" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <StackPanel>
                            <converters:GroupItemsSelector x:Name="selector" Id="{Binding Name}" Source="{StaticResource XamlHeaders}" Watching="IsExpanded"/>
                            <ContentControl Content="{Binding ElementName=selector, Path=Current}" >                                
                            </ContentControl>
                            <ItemsPresenter Visibility="{Binding ElementName=selector, Path=WatchingValue, Converter={x:Static converters:BoolVisiblilityConverter.Instance}}"/>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
于 2013-06-21T05:44:49.483 回答