WPF ListBoxItem去除有焦点时默认改变的背景颜色样式

开发工具与关键技术:Microsoft Visual Studio 2017、WPF

Style 代码:

<SolidColorBrush x:Key="Item.MouseOver.Background" Color="Transparent"/>
<SolidColorBrush x:Key="Item.MouseOver.Border" Color="Transparent"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="Transparent"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="Transparent"/>
<SolidColorBrush x:Key="Item.SelectedActive.Background" Color="Transparent"/>
<SolidColorBrush x:Key="Item.SelectedActive.Border" Color="Transparent"/>

<!--#region 去除默认鼠标移入改变背景颜色的ListBoxItem样式-->
<Style x:Key="ListBoxTitleStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" 
                        Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="False"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<!--#endregion-->


xaml 代码:

<ListBox x:Name="TopNavigationTitle" 
         ScrollViewer.VerticalScrollBarVisibility="Hidden" 
         ItemsSource="{Binding mainModel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
         Margin="50,0,20,0" Background="Transparent" BorderThickness="0" 
         ItemContainerStyle="{StaticResource ListBoxTitleStyle}" 
         ScrollViewer.CanContentScroll="False">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding TitleText}" 
                    x:Name="BtnTitleName" 
                    Margin="-4" 
                    Height="32" 
                    FocusVisualStyle ="{x:Null}" 
                    Style="{StaticResource BtnListBoxTitleStyle}"></Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

效果图:
在这里插入图片描述
这样ListBoxItem默认的背景颜色就去掉了,选中时就只会有你ListBoxItem里面内容控设置的背景颜色。

WPF中,如果你想要设置TabControl中TabItem默认背景色,并且希望在按钮点击后颜色发生变化,你可以按照以下步骤操作: 1. **设置默认背景色**: 首先,在你的项目资源文件(例如:`Resources.xaml`)中添加一个静态资源,给TabItem定义一个默认的Background颜色: ```xml <SolidColorBrush x:Key="DefaultTabColor" Color="#F0F0F0" /> ``` 然后,在TabControl的样式(`TabControl.xaml` 或 `App.xaml` 的 `<Style TargetType="TabControl">` 内部)中应用这个背景色作为模板: ```xml <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TabControl"> <Border Background="{StaticResource DefaultTabColor}"> <ItemsPresenter/> </Border> </ControlTemplate> </Setter.Value> </Setter> ``` 2. **按钮点击事件处理**: 在XAML中,为需要改变背景色的按钮添加一个Click事件处理器。比如,当你点击一个名为`ChangeColorButton`的按钮,可以将当前选中的TabItem的背景色更改为另一种颜色: ```xml <Button Content="Change Color" Click="ChangeColorButton_Click"/> private void ChangeColorButton_Click(object sender, RoutedEventArgs e) { TabItem selectedItem = this.TabControl.SelectedItem as TabItem; if (selectedItem != null) { SolidColorBrush newColor = (SolidColorBrush)Application.Current.Resources["YourNewColorBrush"]; selectedItem.Background = newColor; } } ``` 其中,`YourNewColorBrush`是你在资源文件中定义的新颜色,如`<SolidColorBrush x:Key="NewTabColor" Color="#FF45B8FF" />`。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值