Change ListBoxItem Background Color when mouse is over on the listBoxItem

断了今生、忘了曾经 提交于 2019-12-02 06:48:32

问题


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>

Please help...How??


回答1:


How about a Trigger such as:

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


来源:https://stackoverflow.com/questions/8689703/change-listboxitem-background-color-when-mouse-is-over-on-the-listboxitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!