簡體   English   中英

鼠標懸停時刪除ListboxItem的藍色邊框

[英]Remove blue border of ListboxItem when MouseOver

當鼠標懸停在Listbox中的項目上方並且我用盡了所有想法時,我一直試圖刪除藍色框。也許您會提出的。 先感謝您。 在此處輸入圖片說明

簡單的列表框

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

在此處輸入圖片說明

此行為由控件模板決定。

如果您熟悉XAML,請右鍵單擊ListBox,然后轉到“ Edit Template -> Edit Copy...檢查Border標簽。

為了幫助您,請同時檢查此鏈接: ListBox樣式和模板

出現新問題,我獲得了沒有藍色邊框的列表框

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

也許有人會利用這一點。

沒有x:Key樣式可以在所有TargetType控件中使用。
例如:

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

每次您設置新的Button控件都可以使用。 因此,如果在未指定如下樣式的情況下插入Button<Button/>則它將具有綠色的 Background,如上所述。

另一方面:

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

僅適用於指定Style模板的Button控件。
即: <Button Style="{StaticResource myButton}" /> ->此Button將具有綠色背景,所有其他按鈕將具有默認背景顏色。

我的建議是:始終在樣式中設置ax:Key,以便以后進行設置。 在您的方案中,將x:Key="ContainerStyle"放在第一個代碼中,然后刪除以后聲明的Style。 它應該工作。

暫無
暫無

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

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