3

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:

<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

4

1 回答 1

7

Issue is you are setting Opacity locally as well. Locally set DP always has higher precedence over style setters and style triggers. 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.

于 2014-08-23T08:29:17.650 回答