1

我想在 IsMouseOver 触发器中更改矩形的背景颜色,这可能吗?

<Window>
    <Window.Resources>
        <DataTemplate x:Key="StackListViewItemTemplate">
                <Grid>
                    <Rectangle RadiusX="5" RadiusY="5" >
                        <Rectangle.Fill>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF000000" Offset="0"/>
                                <GradientStop Color="{Binding Path=Events.Colour}" Offset="1"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <TextBlock FontSize="18pt" Grid.RowSpan="2" Text="{Binding Path=Events.Name}" HorizontalAlignment="Center" VerticalAlignment="Center"  />
                </Grid>
            </Grid>
        </DataTemplate>
    </Window.Resources>

    <Grid >
        <ListView ItemTemplate="{DynamicResource StackListViewItemTemplate}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <!-- Here I want to set the background colour of the Rectangle in the DataTemplate -->
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <!-- Same here -->
                        </Trigger>
                    </Style.Triggers>

                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </Grid>

</Window>

编辑:

<Setter Property="Background" Value="Yellow"/>

有没有办法将设置器的属性值绑定到子控件?

4

2 回答 2

1

我使用模板中的数据触发器让它工作

<DataTemplate x:Key="listItemTemplate" DataType="ListViewItem">
    <Grid>
        <Rectangle Name="myRectangle" RadiusX="5" RadiusY="5" >
            <Rectangle.Fill>
                <SolidColorBrush Color="Red" />
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}" Value="True">
            <Setter TargetName="myRectangle" Property="Fill" >
                <Setter.Value>
                    <SolidColorBrush Color="Green" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
于 2009-07-29T09:22:08.080 回答
0

是的,您应该能够执行以下操作:

XAML 教程 - 更改鼠标悬停时的文本颜色

...但当然是一个矩形。

于 2009-07-21T14:31:05.363 回答