根据项目设置ItemControl中每个分组的背景
问题描述:
我有一个项目控件,我用它来分组一个大的彩色按钮。我希望这些组对它们有一个与按钮颜色相匹配的轻微背景,不管它是什么(它是由用户随机定义的,但只有当组中的所有按钮都是相同的时候)。如果组中的所有按钮不具有相同的颜色,该集团的背景应该是透明的。根据项目设置ItemControl中每个分组的背景
<DataTemplate x:Key="ButtonTemplate">
<Button Margin="0,0,8,8" Padding="0" Style="{StaticResource TileButton}" Command="{Binding NavigateToContentsCommand}">
<Grid Height="120" Width="271" Background="{Binding BackgroundBrush}">
<Grid Margin="30">
<TextBlock Grid.Column="1" Style="{StaticResource MediumHeader}" Text="{Binding Name}"/>
</Grid>
</Grid>
</Button>
</DataTemplate>
<ItemsPanelTemplate x:Key="ButtonPanel">
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
<ItemsControl ItemsSource="{Binding Items.View}" ItemTemplate="{StaticResource ButtonTemplate}" ItemsPanel="{StaticResource ButtonPanel}">
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Style="{StaticResource DetailsTextBlock}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" Columns="{Binding Items.View.Groups.Count}"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<Grid VerticalAlignment="Top" HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.RowSpan="2" Background="{BINDING NOTSURE}" Opacity=".2"/>
<ContentPresenter Grid.Row="0"/>
<ItemsPresenter Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ItemsControl.GroupStyle>
</ItemsControl>
当我把绑定不知道是在哪里得到有关如何继续丢失。我不知道我是否需要检查该组或如何去说。任何人有任何这方面的经验的孩子呢? 谢谢!
答
修改边界来触发加载事件
<Border x:Name="ColorBorder" Loaded="ColorBorder_Loaded" Grid.RowSpan="2" Opacity=".2"/>
然后添加此事件以遍历项目,如果它们存在。
private void ColorBorder_Loaded(object sender, RoutedEventArgs e)
{
Brush newBackgroundBrush = Brushes.Transparent;
try
{
var grid = VisualTreeHelper.GetParent((DependencyObject)sender);
GroupItem groupItem = VisualTreeHelper.GetParent(grid) as GroupItem;
CollectionViewGroup collectionViewGroup = (CollectionViewGroup)groupItem.Content;
if (collectionViewGroup.ItemCount > 0)
{
WorkstationNavigationViewModel.NavigationLevelViewModel navigationLevelViewModel = (WorkstationNavigationViewModel.NavigationLevelViewModel)collectionViewGroup.Items[0];
newBackgroundBrush = navigationLevelViewModel.BackgroundBrush;
for (int index = 1; index < collectionViewGroup.ItemCount; index++)
{
navigationLevelViewModel = (WorkstationNavigationViewModel.NavigationLevelViewModel)collectionViewGroup.Items[index];
if (navigationLevelViewModel.BackgroundBrush != newBackgroundBrush)
{
newBackgroundBrush = Brushes.Transparent;
break;
}
}
}
}
catch
{
newBackgroundBrush = Brushes.Transparent;
}
finally
{
Border border = (Border)sender;
border.Background = newBackgroundBrush;
}
}
研究如何使用'TemplatedParent'和'TemplateBinding'。我相信那就是你想要的。 – heltonbiker 2014-11-06 16:18:09
@JonD你分组的属性类型是什么? – dkozl 2014-11-06 16:33:11
这是一个字符串。 “Group” – JonD 2014-11-06 16:48:12