当鼠标悬停在listBoxItem上时,更改ListBoxItem背景颜色 [英] Change ListBoxItem Background Color when mouse is over on the listBoxItem

查看:164
本文介绍了当鼠标悬停在listBoxItem上时,更改ListBoxItem背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鼠标悬停时,我需要为列表项设置更改背景颜色.这是我的代码:

I need to set change background color for list item when mouse is over. Here is my code:

    <DataTemplate x:Key="ListBoxSubCategoryListTemplate" DataType="{x:Type ListBoxItem}">
        <StackPanel>
            <Button x:Name="btnSubCategoryList" Template="{StaticResource subCategoryListItems}" 
                    Content="{Binding Path=sub_category_name}" 
                    Background="Transparent"
                    Height="25"/>
        </StackPanel>
    </DataTemplate>

    <ControlTemplate x:Key="subCategoryListItems" TargetType="{x:Type Button}">
        <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal" >
            <TextBlock Width="150" 
                       Height="{TemplateBinding Button.Height}" 
                       x:Name="textBlockSubCategoryName" 
                       Background="{TemplateBinding Button.Background}" 
                       Text="{TemplateBinding Button.Content}" 
                       FontWeight="Bold" />
            <Image x:Name="img" Width="15" Height="15" Source="/ExpressFurnitureSystem;component/Images/edit.png" ToolTip="Click to edit"></Image>
        </StackPanel>
    </ControlTemplate>

请帮助...如何?

推荐答案

Trigger如:

<DataTemplate x:Key="ListBoxSubCategoryListTemplate" DataType="{x:Type ListBoxItem}"> 
    <StackPanel> 
        <Button x:Name="btnSubCategoryList" Template="{StaticResource subCategoryListItems}"  
                Content="{Binding Path=sub_category_name}"  
                Background="Transparent" 
                Height="25"/> 
    </StackPanel> 
    <DataTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
            <Setter TargetName="btnSubCategoryList" Property="Background" Value="Blue" /> 
        </Trigger> 
    </DataTemplate.Triggers> 
</DataTemplate> 

这篇关于当鼠标悬停在listBoxItem上时,更改ListBoxItem背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆