简体   繁体   English

IsMouseOver WPF中的不透明度

[英]Opacity in IsMouseOver WPF

I'm trying to change the opacity of an item in my StackPanel, but it doesn't seem to do anything, I've searched online for this for about an hour now, and I can't seem to find what I've done wrong... This is my code: 我正在尝试更改StackPanel中某个项目的不透明度,但它似乎无能为力,我已经在网上搜索了大约一个小时,而我似乎找不到我所拥有的东西做错了...这是我的代码:

<Window x:Class="Stations.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:Stations.Model"
        xmlns:l="clr-namespace:Stations"
        Title="SpeedControl" WindowState="Maximized">
    <Window.Resources>
        <m:SpeedControl x:Key="MySpeedControl" />
        <m:NumberToMonthConverter x:Key="Converter" />
        <m:NumberToBrushConverter x:Key="BrushConverter" />
        <Style x:Key="Opacity1" TargetType="StackPanel">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="StackPanel.Opacity" Value="1" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <ItemsControl Margin="10" ItemsSource="{Binding Source={StaticResource MySpeedControl}, Path=DataList}">
        <ItemsControl.Template>
            <ControlTemplate TargetType="ItemsControl">
                <Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
                    <ItemsPresenter/>
                </Border>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <DataTemplate.Resources>
                    <Style TargetType="TextBlock">
                        <Setter Property="FontSize" Value="12"/>
                        <Setter Property="HorizontalAlignment" Value="Center"/>
                    </Style>
                </DataTemplate.Resources>
                <Grid Background="Transparent">
                    <StackPanel Width="145px" Height="45px" Margin="4px" Opacity="{Binding Path=ViolationsRate}" Background="{Binding Path=NumberOfChecks, Converter={StaticResource BrushConverter}}" Style="{StaticResource Opacity1}">
                        <TextBlock Text="{Binding Path=Street}" TextAlignment="Center" Foreground="White"/>
                        <TextBlock Text="{Binding Path=Month, Converter={StaticResource Converter}}"  TextAlignment="Center" Foreground="White"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

As you can see I've maed a Style in , and I call on it in my StackPanel, but the opacity won't change when I hover over the items in it 如您所见,我已经在中添加了样式,然后在StackPanel中对其进行了调用,但是当我将鼠标悬停在样式中时,不透明度不会改变

Issue is you are setting Opacity locally as well. 问题是您也在本地设置不透明度。 Locally set DP always has higher precedence over style setters and style triggers . 本地设置的DP始终比样式设置器和样式触发器具有更高的优先级 Move the local setter to style and it should work: 将本地二传手设置为样式,它应该可以工作:

    <Style x:Key="Opacity1" TargetType="StackPanel">
        <Setter Property="Opacity" Value="{Binding ViolationsRate}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="StackPanel.Opacity" Value="1" />
            </Trigger>
        </Style.Triggers>
    </Style>

Since Style Triggers has higher precedence order style setters so above code will work. 由于样式触发器具有较高的优先级顺序样式设置器,因此上述代码将起作用。

Read more about it here - Dependency Property Value Precedence . 在此处了解更多信息- 依赖属性值优先级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM