1

関連するスタックオーバーフローの投稿で見つけたコードを少し修正しています。トリガーを使用して、IsMouseOver と IsSelected の ListBoxItem の背景に小さな変更を加えました。私のバージョンでは、背景にグラデーションを使用したい:

  <Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Margin" Value="1,2,1,1"/>
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Background" Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid>
                        <Border Background="{TemplateBinding Background}" />
                        <Border Background="LightGray" Margin="0,0">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <!--<Border Margin="2,1,2,0" Grid.Row="0" Background="#57FFFFFF" />-->
                                <Border Margin="2,1,2,0" Grid.Row="0">
                                   <Border.Background >
                                        <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" >
                                            <GradientStop Color="White" Offset="0" />
                                            <GradientStop Color="LightGray" Offset="1" />
                                        </LinearGradientBrush>
                                    </Border.Background>
                                </Border>
                            </Grid>
                        </Border>
                        <ContentPresenter Margin="0,5" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsSelected" Value="False"/>
                            </MultiTrigger.Conditions>
                            <!--<Setter Property="Background" Value="#CCCBAF00" />
                            <Setter Property="Opacity" Value="0.8" />-->
                           <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8">
                                        <GradientStop Color="#CCC9BA5C" Offset="0" />
                                        <GradientStop Color="#CCCBAF00" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                           </Setter>
                        </MultiTrigger>
                        <Trigger Property="IsSelected" Value="True">
                            <!--<Setter Property="Background" Value="#CCCB6400" />
                            <Setter Property="Opacity" Value="0.8" />-->
                            <Setter Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8">
                                        <GradientStop Color="#CCCD8B4C" Offset="0" />
                                        <GradientStop Color="#CCCB6400" Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="ItemContainerStyle" Value="{DynamicResource ListboxItemStyle}" />
        <Setter Property="Margin" Value="3,3,2,1" />
    </Style>

しかし、この変更では機能しません。ありがとう!

4

2 に答える 2

2

切り替えるだけ

<Border Background="LightGray" Margin="0,0">

のようなものに

<Border Background="LightGray" Margin="0,0" Opacity="0.5">

^^Borderシースルーにする

于 2013-06-11T19:08:19.593 に答える
1

1 つの Border コントロールが別のコントロールと重なっていることがわかります。最初の Border (背景がテンプレートにバインドされている) は表示されません。そのため、トリガーで ListBoxItem の背景を変更すると、別の境界線の下に隠されているため、表示されません。境界線コントロールを 1 つだけ持つか、トリガーで 2 つ目の境界線コントロールの可視性を非表示に設定できます。

于 2013-06-11T19:02:30.397 に答える