Search This Blog

Wednesday, July 13, 2011

WPF clone tabItem; WPF create TabItems dynamically; WPF cannot edit TabItems controls; WPF cannot access TabItems controls;

It is impossible to clone TabItems from an existing one without using XamlReader and XamlWriter. You can, however, define a template for creating new TabItems. The template has to be created in your xaml file in Window resources.

  <DataTemplate x:Key="tabItemContent">
            <Canvas Margin="0,0,0,0" Name="tabPracownikCanvas" Visibility="Visible">
                <ListBox Canvas.Left="0" Canvas.Top="-0.004" Height="229.977" Name="listBox1" Width="152.207">
                    <ListBoxItem>Dane osobowe</ListBoxItem>
                <RichTextBox  Canvas.Left="0" Canvas.Top="261.085" Height="246.642" Name="richTextBoxBasicInfo" Width="152.207" IsReadOnly="True"/>
                <Label Canvas.Left="0" Canvas.Top="233.31" Height="27.775" Name="label1" Width="152.207" Content="SomeTextHere">
                <Canvas Canvas.Left="155.54" Canvas.Top="-0.018" Height="507.745" Name="canvasWithDetailedInfo" Width="430.731" >

To add new tabItem just do the following:

TabItem item = new TabItem();
item.Header = "New item";
item.ContentTemplate = TryFindResource("tabItemContent") as DataTemplate;

YOU CANNOT change the contents which were created dynamically just through children editing. You need to bind some properties.

e.g. we will bind label contents:

create a class:

        class Person
            public string Name { get; set; }

            public string Surname { get; set; }

change the label content to binding:

<Label Canvas.Left="0" Canvas.Top="233.31" Height="27.775" Name="label1" Width="152.207" Content="{Binding Name}">

change the Item’s content:

item.Content = new Person() { Name = "dupa", Surname = "xxxx" }

It will automatically change the label’s content when you change item.Content.Name property.



If you like this post, please leave a comment :)