IsMouseOver WPF中的不透明度

时间:2014-08-23 08:10:32

标签: 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>

正如你所看到的,我已经将一个Style加入了,我在StackPanel中调用它,但是当我将鼠标悬停在其中的项目上时,不透明度不会改变

1 个答案:

答案 0 :(得分:3)

问题是您是否在本地设置不透明度。 本地设置DP始终优先于样式设置器和样式触发器 。将本地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>

由于样式触发器具有更高优先级的订单样式设置器,因此上面的代码可以正常工作。

在此处详细了解 - Dependency Property Value Precedence