使用CollectionViewSource可以轻松地对ListBoxItem进行分组。 但每组Header,只有一个“Name”数据。 如何将更多数据添加到标题?
像图片一样。<ItemsPresenter/>
,groupItem的part.if我想要绑定它,
我该怎么办?和我的代码:
<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>
答案 0 :(得分:0)
我做了一个包装类,但觉得非常难看。还有其他办法吗?
1,从
获取Transition类<强> 2.and 强>
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>