2014-11-06 73 views
0

我有一個項目控件,我用它來分組一個大的彩色按鈕。我希望這些組對它們有一個與按鈕顏色相匹配的輕微背景,不管它是什麼(它是由用戶隨機定義的,但只有當組中的所有按鈕都是相同的時候)。如果組中的所有按鈕不具有相同的顏色,該集團的背景應該是透明的。根據項目設置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> 

當我把綁定不知道是在哪裏得到有關如何繼續丟失。我不知道我是否需要檢查該組或如何去說。任何人有任何這方面的經驗的孩子呢? 謝謝!

+0

研究如何使用'TemplatedParent'和'TemplateBinding'。我相信那就是你想要的。 – heltonbiker 2014-11-06 16:18:09

+0

@JonD你分組的屬性類型是什麼? – dkozl 2014-11-06 16:33:11

+0

這是一個字符串。 「Group」 – JonD 2014-11-06 16:48:12

回答

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; 
    } 

} 
相關問題