简体   繁体   中英

WPF TabItem (Custom Control) does not apply style when add in runtime

Friends, I have a tabcontrol with the following resource:

<TabControl>
   <TabControl.Resources>
      <Style TargetType="{x:Type TabItem}">
         <!-- setters etc -->
      </Style>
   </TabControl.Resources>

   <TabItem Header="FILES"/>
   <TabItem Header="DEBUG"/>
</TabControl>

The tabItem elements that are added from the xaml do display the style, the problem I have is that when I add a tabitem (wpf usercontrol) at runtime it does not apply the style, here an example with two cases.

  • case 1 (it works)
 var tab = new TabItem() { Header = "OTHER MENU", IsSelected = true }; tabcontrol.Items.Add(tab);
  • case 2 (doesn't work)
 var tab = new Modules.Customer.tab__list() { IsSelected = true }; tabcontrol.Items.Add(tab);

Modules.Customer.tab__list is this:

XAML code

<TabItem x:Class="Solucion.Presentacion.Modulos.Perfil.tab__listado" 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" xmlns:local="clr-namespace:Solucion.Presentacion.Modulos.Perfil" mc:Ignorable="d" Header="Perfiles"> <!-- CODE --> </TabItem>

C# code

public partial class tab__list: TabItem { public tab__list() { InitializeComponent(); } }

Any idea how to fix this.

EDIT:

When i add control in xaml the style doesn't work.

 <TabControl> <TabControl.Resources> <Style TargetType="{x:Type TabItem}"> <.-- setters etc --> </Style> </TabControl:Resources> <TabItem Header="FILES"/> <TabItem Header="DEBUG"/> <ctrl:tab__list/> <!-- DOESN'T WORK TOO --> </TabControl>

Customer.tab__list" should be a regular class, it doesn't need to be XAML file. Create a new regular class. it should be "tab__list.cs"

public class MyTabItem : TabItem
{
    public MyTabItem()
    {
        Header = "Perfiles";
    }
}

I made something like that.

   <TabControl x:Name="TabControl">
        <TabItem Header="one regular tab">

        </TabItem>
    </TabControl>

And then i made

TabControl.Items.Add(new MyTabItem());

Result: 在此处输入图像描述

I made something what you asked more. Created an usercontrol and put textbox inside it.

<UserControl x:Class="DemoThings.UserControl1"
         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" 
         xmlns:local="clr-namespace:DemoThings"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
                  <TextBox Text="MY USER CONTROL"></TextBox>  
</Grid>

And when i add the tabitem i use like that.

            TabControl.Items.Add(new MyTabItem()
        {
            Content = new Grid()
            {
                Children = { new UserControl1() }
            }
        });

Result: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM