簡體   English   中英

IsMouseOver WPF中的不透明度

[英]Opacity in IsMouseOver WPF

我正在嘗試更改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始終比樣式設置器和樣式觸發器具有更高的優先級 將本地二傳手設置為樣式,它應該可以工作:

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

由於樣式觸發器具有較高的優先級順序樣式設置器,因此上述代碼將起作用。

在此處了解更多信息- 依賴屬性值優先級

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM