c# - IsMouseOver WPF 中的不透明度

标签 c# wpf xaml ismouseover

我正在尝试更改 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>

如您所见,我在 中制作了一个样式,并在我的 StackPanel 中调用它,但是当我将鼠标悬停在其中的项目上时,不透明度不会改变

最佳答案

问题是您也在本地设置不透明度。 本地设置的 DP 总是比样式 setter 和样式触发器具有更高的优先级。将本地 setter 移动到样式,它应该可以工作:

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

由于 Style Triggers 具有更高的优先顺序样式 setter ,因此上面的代码将起作用。

在这里阅读更多相关信息 - Dependency Property Value Precedence .

关于c# - IsMouseOver WPF 中的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25459878/

相关文章:

c# - 如何防止这种布局循环?

c# - C#中switch语句的多个变量

c# - 具有重复字符的子串

c# - LINQ to SQL 业务对象创建最佳实践

c# - C# 循环后如何获取最后的结果

c# - 包含 MaterialDesignInXaml 后无法使用 TextBox

c# - 如何在 GroupBox 标题中包装文本?

wpf - 如何在 wpf 应用程序中运行应用程序?

wpf - 我真的需要 MVVM 吗?

c# - 具有嵌套类型的 XAML DataContext DesignInstance