Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF在触发器中设置ListBoxItem背景_Wpf_Background_Triggers - Fatal编程技术网

WPF在触发器中设置ListBoxItem背景

WPF在触发器中设置ListBoxItem背景,wpf,background,triggers,Wpf,Background,Triggers,我正在对我在相关stackoverflow帖子上找到的代码做一点修改。我在IsMouseOver和IsSelected上使用触发器对ListBoxItem的背景进行了细微的更改。在我的版本中,我希望使用渐变作为背景: <Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Margin" Value="1,2,1,1"/>

我正在对我在相关stackoverflow帖子上找到的代码做一点修改。我在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>


但这种修改不起作用。谢谢

我可以看到一个边界控件与另一个重叠。第一个边框(其背景绑定到模板)将永远不可见。因此,当您在触发器中更改ListBoxItem的背景时,您看不到它,因为它隐藏在另一个边框下。 您可以只使用一个边框控件,也可以将第二个边框控件的可见性设置为隐藏在触发器中。

只需切换即可

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

差不多

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

使^^
边框
透明