我有一个itemscontrol,我用它来分组大型彩色按钮。我希望这些组具有与按钮颜色匹配的轻微背景,无论它是什么(它是由用户随机定义的,但只有当组中的所有按钮都相同时才会这样。如果全部组中的按钮颜色不同,组的背景应该是透明的。
<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>
我把Binding放在哪里不确定是我迷失在何处继续。我不确定我是否需要检查小组的孩子或如何去做。有人对此有经验吗? 谢谢!
答案 0 :(得分:0)
修改边框以触发加载的事件
<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;
}
}