单击ItemsControl时更改网格背景颜色

时间:2017-12-12 12:22:37

标签: wpf xaml itemscontrol

我在ItemsControl中有以下Grid

 <Grid Grid.Row="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
 <Border Grid.Row="0" Grid.Column="0" Margin="5" BorderThickness="1" BorderBrush="{StaticResource ControlBorder}" Background="{StaticResource ControlBackground}">
    <ItemsControl ItemsSource="{Binding MyCollection}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="2"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ContentControl Content="{Binding}" Margin="1"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
 </Border>

只要点击ItemsControl中的项目鼠标,我就想更改网格单元格的背景。我该怎么做?

2 个答案:

答案 0 :(得分:1)

您可以使用Grid事件在ContentControl之外定义MouseLeftButtonDown 如果它的Clicked改变你的网格背景

<强>的Xaml:

 <Grid x:Name="MyGrid" Grid.Row="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Grid.Row="0" Grid.Column="0" Margin="5" BorderThickness="1" BorderBrush="{StaticResource ControlBorder}" Background="{StaticResource ControlBackground}">
            <ItemsControl ItemsSource="{Binding MyCollection}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="2"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid x:Name="GridInsideDataTemplate" VerticalAlignment="Top" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
                            <ContentControl Content="{Binding}" Margin="10"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Border>

    </Grid>

<强>代码:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    MyGrid.Background = Brushes.Green;
}

<强>编辑:

因为你希望在XAML中完全这样做。

对KeyTime为“0”的MouseDown事件使用事件触发器

<DataTemplate>
    <Grid x:Name="GridInsideDataTemplate" VerticalAlignment="Top">
      <Grid.Style>
        <Style TargetType="Grid">
           <Setter Property="Background">
             <Setter.Value>
                <SolidColorBrush Color="Yellow"/>
             </Setter.Value>
           </Setter>
        <Style.Triggers>
           <EventTrigger RoutedEvent="MouseDown">
               <BeginStoryboard>
                  <Storyboard>                                                    
                     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)">
                        <EasingColorKeyFrame KeyTime="0" Value="Green" />                                                    
                     </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                   </BeginStoryboard>
                </EventTrigger>
              </Style.Triggers>
            </Style>
          </Grid.Style>

         <ContentControl Content="{Binding}" Margin="10"/>

   </Grid>
 </DataTemplate>

或者您可以将样式定义为资源,并将其用于任何Grid

<Grid x:Name="GridInsideDataTemplate"
      Style="{StaticResource GridStyle}"      
      VerticalAlignment="Top">

          <ContentControl Content="{Binding}" Margin="10"/>
</Grid>

答案 1 :(得分:1)

您可以处理MouseLeftButtonDownContentControl的{​​{1}}事件,在可见树中找到父ItemTemplate并更改此Border之一:

Background

A&#34; cell&#34;由private void ContentControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ItemsControl ic = FindParent<ItemsControl>(sender as DependencyObject); if (ic != null) { Border border = ic.Parent as Border; if (border != null) border.Background = Brushes.Blue; } } private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject { var parent = VisualTreeHelper.GetParent(dependencyObject); if (parent == null) return null; var parentT = parent as T; return parentT ?? FindParent<T>(parent); } 的特定位置中的元素定义,即本例中的Grid元素。