1

当鼠标悬停在列表框中的项目上时,我一直在尝试删除蓝色框,但我没有想法,也许你会想出任何想法。先感谢您。在此处输入图像描述

简单的列表框

<ListBox ItemsSource="{Binding Mylist}" />

不幸的是,下面的解决方案不起作用

<ListBox ItemsSource="{Binding lista}" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>

    </ListBox>

在此处输入图像描述

4

3 回答 3

1

此行为由控制模板规定。

如果您熟悉 XAML,请右键单击 ListBox,转到Edit Template -> Edit Copy...检查Border标签。

为了帮助您,请查看此链接:ListBox Styles and Templates

于 2013-10-02T18:21:24.553 回答
0

新问题出来了,我获取了没有蓝色边框的列表框

 <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Top" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter
                          x:Name="contentPresenter"
                          Content="{TemplateBinding Content}"
                          ContentTemplate="{TemplateBinding ContentTemplate}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          Margin="{TemplateBinding Padding}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

但我已经像这样设置了 ItemContainerStyle

 <Style TargetType="ListBoxItem" x:Key="ContainerStyle">
        <Setter Property="ContentTemplate" Value="{StaticResource not_mouseover}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="ContentTemplate" Value="{StaticResource mouseover}"/>
            </Trigger>

        </Style.Triggers>
    </Style>
    <ListBox ItemsSource="{Binding lista}" ItemContainerStyle="{StaticResource ContainerStyle}">

在这种情况下,事实证明它不起作用(我的意思是蓝色边框像以前一样出现)。如果我将 ItemTemplate 设置为任何指定的 DateTemplate 它工作正常,但这里不是。你碰巧知道为什么吗?我整理了这个。ListboxItem 只有一种样式

<Style x:Key="item_template" TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Template" Value="{StaticResource control_mouseover}"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="Template" Value="{StaticResource control_not_mouseover}"/>
            </Trigger>
        </Style.Triggers>           
    </Style>
</Window.Resources>
    <ListBox ItemsSource="{Binding lista}" ItemContainerStyle="{StaticResource item_template}"> 
    </ListBox>

并声明 ControlTemplate 以删除蓝色边框

<ControlTemplate x:Key="control_not_mouseover" TargetType="ListBoxItem">
        <ContentPresenter
                          Content="{TemplateBinding Content}"
                          ContentTemplate="{StaticResource not_mouseover}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          Margin="{TemplateBinding Padding}"/>
    </ControlTemplate>
    <ControlTemplate x:Key="control_mouseover" TargetType="ListBoxItem">
            <ContentPresenter
                          Content="{TemplateBinding Content}"
                          ContentTemplate="{StaticResource mouseover}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          Margin="{TemplateBinding Padding}"/>
        </ControlTemplate>

也许有人会利用这一点。

于 2013-10-02T19:58:32.087 回答
0

不带 no 的样式x:Key适用于所有 TargetType 控件。
例如:

 <Style TargetType="Button">
        <Setter Property="Background" Value="Green" />
 </Style>

每次设置新Button控件时都会起作用。因此,如果您插入 aButton而不指定这样的样式:<Button/>它将具有绿色背景,如上所述。

另一方面:

 <Style TargetType="Button" x:Key="myButton">
        <Setter Property="Background" Value="Green" />
 </Style>

仅适用于Button指定Style模板的控件。
即:<Button Style="{StaticResource myButton}" />-> 这Button将具有绿色背景,所有其他按钮将具有默认背景颜色。

我的建议是:始终在您的样式中设置 ax:Key,以便以后设置它们。在您的场景中,将第一个代码放在x:Key="ContainerStyle"第一个代码处,然后删除稍后声明的样式。它应该工作。

于 2013-10-02T20:37:15.240 回答