WPF的TabControl,改变TabItem的背景颜色与C#代码 [英] WPF TabControl, change the background color of the TabItem with C# codes

查看:2162
本文介绍了WPF的TabControl,改变TabItem的背景颜色与C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我认为这是一个初学者的问题。我找遍了所有的相关问题。但是,所有的人都用的.xaml回答。不过,我需要的是后面的代码。
我有一个TabControl。我需要设置其项目的背景颜色。
我需要时选择它的项目设置不同的颜色,未选和悬停。
非常感谢你的帮助。
我想在这里发表我的代码。但是,目前,我所是的TabControl的一个实例,并呼吁ActiveTabIndex一个属性。



---------------- -----------------------编辑1 ------------------------- ----------------------



我添加了一个事件的SelectionChanged



(this.myTabControl为System.Windows.Controls.TabControl).SelectionChanged + = TabSet_SelectionChanged;

 无效TabSet_SelectionChanged(对象发件人,System.Windows.Controls.SelectionChangedEventArgs E)
{
的foreach(在(this.myTabControl为System.Windows.Controls.TabControl)System.Windows.Controls.TabItem项目。项)
{
如果(项目==(this.myTabControl为System.Windows.Controls.TabControl).SelectedItem)
{
item.Background = System.Windows.Media .Brushes.Red;
}
,否则
item.Background = System.Windows.Media.Brushes.Green;
}
}



不过,我只能设置绿色其实。所选项目的背景颜色保持作为默认颜色而不是红色。这个任何提示?
另外,我想知道如何为悬停添加事件。还没有找到确切的事件。再次感谢。



-----------------------编辑2 ------- -----------------------



很长很长很长的讨论之后。
我已经决定了(其实不是我的决定)来使用XAML。是啊。我是新来WPF。所以我还是有这个问题(我是最适合这个很抱歉,请紧我)。这些问题在这里:
我想背景颜色变为橙色,当鼠标移动到TabItem的。现在的颜色是橙色,当鼠标移动到的ContentPanel和TabItem的。我需要它是橙色当鼠标悬停只有TabItem的。 (我不知道我是不够清楚。)
另一个问题是,我将让用户设置的颜色,而不是设置为红色,直接。我需要一些绑定,我认为。对于这个问题,我会在稍后谷歌肯定的。只是要明确。
非常感谢你对在座的各位。 。真正有用的

 < TabItem的X:类=MyControls.Tab
的xmlns =HTTP:// schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:MC =HTTP: //schemas.openxmlformats.org/markup-compatibility/2006
的xmlns:D =http://schemas.microsoft.com/expression/blend/2008
MC:可忽略=D
D:DesignHeight =300D:DesignWidth =300>
< TabItem.Style>
<风格的TargetType ={X:类型TabItem的}>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型TabItem的}>
<网格和GT;
< BORDER NAME =边框保证金=0,0,-4,0了borderThickness =1,1,1,1CornerRadius =2,12,0,0>
< ContentPresenter X:NAME =ContentSiteVerticalAlignment =中心的Horizo​​ntalAlignment =中心ContentSource =头保证金=12,2,12,2RecognizesAccessKey =真/>
< /边框>
< /网格和GT;
< ControlTemplate.Triggers>

<触发属性=IsSelectedVALUE =真>
< setter属性=Panel.ZIndexVALUE =100/>
<二传手的TargetName =边框属性=背景VALUE =红/>
<二传手的TargetName =边框属性=了borderThicknessVALUE =1,1,1,0/>
< /触发>

<触发属性=IsSelectedVALUE =FALSE>
<二传手的TargetName =边框属性=背景值=绿色/>
< /触发>
<触发属性=IsMouseOverVALUE =真>
<二传手的TargetName =边框属性=背景值=橙色/>
< /触发>

< /ControlTemplate.Triggers>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /TabItem.Style>
< / TabItem的>



-------------编辑3 ------ ----------



我不太清楚,我认为。
这是它现在是什么:
它的工作很好,如果鼠标在其它标签:
在这里输入的形象描述



不过,当鼠标在圆形区域,选定项目的背景颜色应该是红色的,而不是橘子:



- -------------编辑4 -------------------



谢谢各位的回复。
现在后我的用户和其他一些长时间的讨论,我们要动态地改变背景颜色。用户希望由自己来设置颜色。基本上,我需要首先做一些结合(如果这是这个词)。我试过以下。然而,所选择的标签不与红色的背景。我用史努比退房,事实证明,背景是本地设置为红色。我有点困惑在这里。我问过身边,有人给了我使用TemplateBinding,因为它的ControlTemplate下的建议。但是,我想尽量创造依赖属性和类似的东西。只是我不明白,为什么我应该使用TemplateBinding,因为我看了一些文章说这是编译时绑定。我完全糊涂了。我是新来WPF,我很抱歉,如果问题是水平低的问题。
再次感谢

 < TabItem的X:!类=MyControl.Tab
的xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:MC =http://schemas.openxmlformats.org/markup-compatibility/2006
的xmlns:D =http://schemas.microsoft.com/expression/blend/2008
MC:可忽略=D
D:DesignHeight =300D:DesignWidth =300>
< TabItem.Style>
<风格的TargetType ={X:类型TabItem的}>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型TabItem的}>
<网格和GT;
< BORDER NAME =边框保证金=0,0,-4,0了borderThickness =1,1,1,1CornerRadius =2,12,0,0>
< ContentPresenter X:NAME =ContentSiteVerticalAlignment =中心的Horizo​​ntalAlignment =中心ContentSource =头保证金=12,2,12,2RecognizesAccessKey =真/>
< /边框>
< /网格和GT;
< ControlTemplate.Triggers>
<触发属性=IsSelectedVALUE =真>
< setter属性=Panel.ZIndexVALUE =100/>
<二传手的TargetName =边框属性=背景VALUE ={结合SelectedBgClr}/>
< setter属性=前景VALUE =黄色/>
<二传手的TargetName =边框属性=了borderThicknessVALUE =1,1,1,0/>
< /触发>
<触发属性=IsSelectedVALUE =FALSE>
<二传手的TargetName =边框属性=背景值=绿色/>
< setter属性=前景VALUE =艾莉斯蓝/>
< /触发>
<触发属性=IsMouseOverVALUE =真>
<二传手的TargetName =边框属性=背景值=橙色/>
< setter属性=前景VALUE =黑/>
< /触发>
< /ControlTemplate.Triggers>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /TabItem.Style>
< / TabItem的>



背后的代码是:



 公共选项卡()
{
SelectedBgClr =新的SolidColorBrush(Colors.Red);
// UnSelectedBgClr =新的SolidColorBrush(Colors.Green);
// HoverBgClr =新的SolidColorBrush(Colors.Orange);
的InitializeComponent();

}
公共静态只读的DependencyProperty SelectedBgClrProperty = DependencyProperty.Register(SelectedBgClr的typeof(刷)的typeof(表),新UIPropertyMetadata(NULL));
公共刷SelectedBgClr
{
得到
{
返回(刷)的GetValue(SelectedBgClrProperty);
}

{
的SetValue(SelectedBgClrProperty,值);
}
}


解决方案

WPF使您可以创建基于现有的控制新的自定义控制类型,则可以与Microsoft网站上的模板/样式声明填好并更改位适合你。创建一个名为MyTabControl一个新的用户控件以及与此替换的背后代码:

 公共部分类MyTabControl:TabControl的
{
公共静态只读的DependencyProperty SelectedBgClrProperty = DependencyProperty.Register(SelectedBgClr,
的typeof(刷)的typeof(MyTabControl),新UIPropertyMetadata(NULL));

[类别(外观)]
公共刷SelectedBgClr
{
得到
{
返回(刷)的GetValue(SelectedBgClrProperty) ;
}

{
的SetValue(SelectedBgClrProperty,值);
}
}

公共MyTabControl()
{
的InitializeComponent();
}
}

现在替换这个XAML(你需要更改命名空间无论你的项目):

 < TabControl的X:类=TabDemo.MyTabControl
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:MC =http://schemas.openxmlformats.org/markup-compatibility/2006
的xmlns:D =http://schemas.microsoft.com/expression/blend/2008
MC:可忽略=D
NAME =TabControl的
D:DesignHeight =300D:DesignWidth =300>

< TabControl.Resources>

<风格的TargetType ={X:类型TabItem的}>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型TabItem的}>
<网格和GT;
< BORDER NAME =边框保证金=0,0,-4,0了borderThickness =1,1,1,1CornerRadius =2,12,0,0>
< ContentPresenter X:NAME =ContentSiteVerticalAlignment =中心的Horizo​​ntalAlignment =中心ContentSource =头保证金=12,2,12,2RecognizesAccessKey =真/>
< /边框>
< /网格和GT;
< ControlTemplate.Triggers>
<触发属性=IsSelectedVALUE =真>
< setter属性=Panel.ZIndexVALUE =100/>
<二传手的TargetName =边框属性=背景VALUE ={绑定的ElementName = TabControl的,路径= SelectedBgClr}/>
< setter属性=前景VALUE =黄色/>
<二传手的TargetName =边框属性=了borderThicknessVALUE =1,1,1,0/>
< /触发>
<触发属性=IsSelectedVALUE =FALSE>
<二传手的TargetName =边框属性=背景值=绿色/>
< setter属性=前景VALUE =艾莉斯蓝/>
< /触发>
<触发属性=IsMouseOverVALUE =真>
<二传手的TargetName =边框属性=背景值=橙色/>
< setter属性=前景VALUE =黑/>
< /触发>
< /ControlTemplate.Triggers>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /TabControl.Resources>

< TabControl.Style>
<风格的TargetType ={X:类型的TabControl}>
< setter属性=OverridesDefaultStyle
值=真/>
< setter属性=SnapsToDevicePixels
值=真/>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型的TabControl}>
<电网KeyboardNavigation.TabNavigation =本地>
< Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition高度=*/>
< /Grid.RowDefinitions>
< VisualStateManager.VisualStateGroups>
< VisualStateGroup X:NAME =CommonStates>
<的VisualState X:名称=已禁用>
<情节提要>
< ColorAnimationUsingKeyFrames Storyboard.TargetName =边框
Storyboard.TargetProperty =。(Border.BorderBrush)
(SolidColorBrush.Color)>
将; EasingColorKeyFrame KeyTime =0
值=#FFAAAAAA/>
< / ColorAnimationUsingKeyFrames>
< /故事板>
< /&的VisualState GT;
< / VisualStateGroup>
< /VisualStateManager.VisualStateGroups>
将;的TabPanel X:名称=HeaderPanel
Grid.Row =0
Panel.ZIndex =1
保证金=0,0,4, - 1
IsItemsHost =真
KeyboardNavigation.TabIndex =1/>
< BORDER X:名称=边框
Grid.Row =1
了borderThickness =1
CornerRadius =2
KeyboardNavigation.TabNavigation =本地
KeyboardNavigation.DirectionalNavigation =含有
KeyboardNavigation.TabIndex =2背景={绑定的ElementName = TabControl的,路径= SelectedBgClr}>
< ContentPresenter X:NAME =PART_SelectedContentHost
保证金=4
ContentSource =SelectedContent/>
< /边框>
< /网格和GT;
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /TabControl.Style>





在你的主窗口或任何现在只是用它,你会经常的TabControl,在SelectedBgClr同时设置选定的选项卡头和主面板背景(如果你在XAML看你上面已经看到两者绑定):

 <局部:MyTabControl SelectedBgClr =红> 
< TabItem的标题=富/>
< TabItem的标题=酒吧/>
< TabItem的标题=巴兹/>
< /地方:MyTabControl>



注意,后面的代码是最小的,它仍然是XAML是在做大量的工作和MyTabControl的只是用作依赖项属性的包装。在实际的应用中,你将使用一种叫做附加属性,这样你就不需要推导出一个全新的TabControl类。


Hi I think this is a beginners questions. I've searched all the related questions. But all of them are answered by .xaml. However, what I need is the back code. I have a TabControl. I need to set the background color of its items. I need to set different colors for the items when it is selected, unselected and hover. Thank you so much for your help. I want to post my codes here. However, currently, all I have is an instance of the TabControl and one property called ActiveTabIndex.

---------------------------------------Edit 1-----------------------------------------------

I have added an event SelectionChanged

(this.myTabControl as System.Windows.Controls.TabControl).SelectionChanged += TabSet_SelectionChanged;

void TabSet_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            foreach (System.Windows.Controls.TabItem item in (this.myTabControl as System.Windows.Controls.TabControl).Items)
            {
                if (item == (this.myTabControl as System.Windows.Controls.TabControl).SelectedItem)
                {
                    item.Background = System.Windows.Media.Brushes.Red;
                }
                else
                    item.Background = System.Windows.Media.Brushes.Green;
            }
        }

However, I can only set the Green actually. The background color of the selected item keeps as the default color instead of red. Any hints about this? Also, I would like to know how to add event for the hover. Haven't find the exact event. Thanks again.

-----------------------Edit 2------------------------------

After a long long long discussion. I've decided (actually not my decision) to use the XAML. Yeah. I am new to WPF. So I still have questions about this (I am so sorry for this, Please bear me). The questions are here: I would like to change the background color to Orange when the mouse is over the TabItem. Now the color is Orange when the mouse is over the ContentPanel and TabItem. I need it to be orange when the mouse is over the TabItem only. (I am not sure I am clear enough.) Another question is that I would let users to set the color instead of setting is to red directly. I need some bindings I think. For this question, I will google later for sure. Just want to be clear. Thank you so much for all of you. Really helpful.

<TabItem x:Class="MyControls.Tab"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <TabItem.Style>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border  Name="Border" Margin="0,0,-4,0" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" >
                                <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>

                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="Border" Property="Background" Value="Red" />
                                <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                            </Trigger>

                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="Green" />                                
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="Orange" />
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabItem.Style>
</TabItem>

------------- Edit 3 ----------------

I am not clear enough I think. Here is what it is now: It's working fine if the mouse is over other tabs:

But When the mouse is over the circled area, the background color of the selected item should be red instead of orange:

---------------Edit 4 -------------------

Thanks for all of you for your reply. Now after a long discussion with my users and some others, we would like to change the background color dynamically. The user wants to set the color by themselves. Basically, I need first do some binding (if this is the term). I've tried the following. However, the selected tab is not with red background. I used the Snoop to check out, it turns out that the Background is set as red locally. I am a little confusing here. I've asked around, and someone gave me the suggestion to use TemplateBinding since it is under ControlTemplate. But, I've tried to create dependency property and something like that. But just I don't understand why should I use TemplateBinding since I read some article said that it is for compile time binding. I am totally confused. I am new to WPF, I am sorry if the question is low level question. Thanks again!

<TabItem x:Class="MyControl.Tab"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <TabItem.Style>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border  Name="Border" Margin="0,0,-4,0" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" >
                                <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="Border" Property="Background" Value="{Binding SelectedBgClr}" />
                                <Setter Property="Foreground" Value="Yellow" />
                                <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                            </Trigger>                            
                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="Green" /> 
                                <Setter Property="Foreground" Value="AliceBlue"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="Orange" />
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabItem.Style>
</TabItem>

The behind code is:

 public Tab()
        {
            SelectedBgClr = new SolidColorBrush(Colors.Red);
            //UnSelectedBgClr = new SolidColorBrush(Colors.Green);
            //HoverBgClr = new SolidColorBrush(Colors.Orange);
            InitializeComponent();

        }
public static readonly DependencyProperty SelectedBgClrProperty = DependencyProperty.Register("SelectedBgClr", typeof(Brush), typeof(Tab), new UIPropertyMetadata(null));
public Brush SelectedBgClr
{
    get
    {
        return (Brush)GetValue(SelectedBgClrProperty);
    }
    set
    {
        SetValue(SelectedBgClrProperty, value);
    }
}

解决方案

WPF lets you create a new custom control type based on an existing control, you can then fill it out with the template/style declaration on the Microsoft site and change the bits to suit you. Create a new user control called MyTabControl and replace the behind code with this:

public partial class MyTabControl : TabControl
{
    public static readonly DependencyProperty SelectedBgClrProperty = DependencyProperty.Register("SelectedBgClr",
        typeof(Brush), typeof(MyTabControl), new UIPropertyMetadata(null));

    [Category("Appearance")]
    public Brush SelectedBgClr
    {
        get
        {
            return (Brush)GetValue(SelectedBgClrProperty);
        }
        set
        {
            SetValue(SelectedBgClrProperty, value);
        }
    }

    public MyTabControl()
    {
        InitializeComponent();
    }
}

Now replace the xaml with this (you'll need to change the namespace to whatever your project is):

<TabControl x:Class="TabDemo.MyTabControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         Name="tabControl"
         d:DesignHeight="300" d:DesignWidth="300">

<TabControl.Resources>

    <Style TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border  Name="Border" Margin="0,0,-4,0" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" >
                            <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Panel.ZIndex" Value="100" />
                            <Setter TargetName="Border" Property="Background" Value="{Binding ElementName=tabControl, Path=SelectedBgClr}" />
                            <Setter Property="Foreground" Value="Yellow" />
                            <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="Green" />
                            <Setter Property="Foreground" Value="AliceBlue"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="Orange" />
                            <Setter Property="Foreground" Value="Black"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.Resources>

<TabControl.Style>
    <Style  TargetType="{x:Type TabControl}">
        <Setter Property="OverridesDefaultStyle"
          Value="True" />
        <Setter Property="SnapsToDevicePixels"
          Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabControl}">
                    <Grid KeyboardNavigation.TabNavigation="Local">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                Storyboard.TargetProperty="(Border.BorderBrush).
                    (SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                         Value="#FFAAAAAA" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <TabPanel x:Name="HeaderPanel"
                    Grid.Row="0"
                    Panel.ZIndex="1"
                    Margin="0,0,4,-1"
                    IsItemsHost="True"
                    KeyboardNavigation.TabIndex="1" />
                        <Border x:Name="Border"
                  Grid.Row="1"
                  BorderThickness="1"
                  CornerRadius="2"
                  KeyboardNavigation.TabNavigation="Local"
                  KeyboardNavigation.DirectionalNavigation="Contained"
                  KeyboardNavigation.TabIndex="2" Background="{Binding ElementName=tabControl, Path=SelectedBgClr}">
                            <ContentPresenter x:Name="PART_SelectedContentHost"
                              Margin="4"
                              ContentSource="SelectedContent" />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.Style>

Now in your MainWindow or whatever just use it as you would a regular TabControl, the SelectedBgClr will set both the selected tab header and the main panel background (if you look in the XAML above you've see bindings for both):

<local:MyTabControl SelectedBgClr="Red">
        <TabItem Header="Foo"  />
        <TabItem Header="Bar" />
        <TabItem Header="Baz" />
    </local:MyTabControl>

Notice that the behind-code is minimal, it's still XAML that's doing the bulk of the work and MyTabControl is simply used as a wrapper for the dependency property. In a real application you would use something called an attached property so that you wouldn't need to derive a whole new TabControl class.

这篇关于WPF的TabControl,改变TabItem的背景颜色与C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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