在运行时更改网格的背景

时间:2015-10-05 09:46:13

标签: wpf background grid

我的网格设置为浅灰色背景,我想知道当鼠标悬停在特定网格上时如何更改网格的背景。如果我将目标类型从边框改为网格,我目前最终将我的屏幕全黑:

我得到了什么

enter image description here

我想要什么,但所有浅灰色都是黄色

enter image description here

XAML

<DataTemplate x:Key="PropertyNodeTemplate">
        <Grid VerticalAlignment="Top" HorizontalAlignment="Center" Background="LightGray">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Border VerticalAlignment="Center" Grid.Row="0">
                <Border.Style>
                    <Style TargetType="Border">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsHighlighted}" Value="True">
                                <Setter Property="Background" Value="Yellow"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <Label Name="PropertyTitle" Content="Property Shnarf" VerticalAlignment="Top" HorizontalAlignment="Center">
                </Label>
            </Border>
            <Button Name="AddScenario" Click="AddScenarioButton_OnClick" Grid.Row="3" BorderThickness="0">
                <Button.Style>
                    <Style TargetType="Button">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="Button">
                                    <Border BorderThickness="0">
                                        <ContentPresenter/>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsHighlighted}" Value="True">
                                <Setter Property="Visibility" Value="Visible"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsHighlighted}" Value="False">
                                <Setter Property="Visibility" Value="Hidden"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
                <Border BorderThickness="1,1,1,1" VerticalAlignment="Center" Background="LightGray">
                    <Border.BorderBrush>
                        <DrawingBrush Viewport="8,8,8,8" ViewportUnits="Absolute" TileMode="Tile">
                            <DrawingBrush.Drawing>
                                <DrawingGroup>
                                    <GeometryDrawing Brush="LightGray">
                                        <GeometryDrawing.Geometry>
                                            <GeometryGroup>
                                                <RectangleGeometry Rect="0,0,50,50" />
                                                <RectangleGeometry Rect="50,50,50,50" />
                                            </GeometryGroup>
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingGroup>
                            </DrawingBrush.Drawing>
                        </DrawingBrush>
                    </Border.BorderBrush>
                    <StackPanel Margin="2" Width="120" Orientation="Horizontal" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type syncfusion:Node}}}">
                        <Image Source="{StaticResource ImageBkgPlus}" Width="16" Height="16" Margin="5"/>
                        <Label Width="70" FontSize="10" VerticalAlignment="Center" Content="Add Scenario">
                        </Label>
                    </StackPanel>
                </Border>
            </Button>
        </Grid>
    </DataTemplate>

1 个答案:

答案 0 :(得分:2)

您的Border仅位于网格的第0行,因此这是唯一一个响应DataTrigger的部分。为什么不使用Grid.Style而不是Border.Style

<DataTemplate x:Key="PropertyNodeTemplate">
    <Grid VerticalAlignment="Top" HorizontalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.Style>
          <Style TargetType="Grid">
            <Setter 
               Property="BackGround"
               Value="LightGray" />
            <Style.Triggers>
              <DataTrigger 
                Binding="{Binding IsHighlighted}" 
                value="True">
                <Setter 
                   Property="Background" 
                   Value="Yellow" />
              </DataTrigger>
            </Style.Triggers>
          </Style>
        </Border.Style>
      ...
    </Grid>
</DataTemplate>

您可以使用IsMouseover而不是DataTrigger,具体取决于您的需求:WPF Grid IsMouseOver Property