IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Copier le contenu d'un TabItem [Débutant]


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Technicien
    Inscrit en
    Juin 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Juin 2014
    Messages : 19
    Par défaut Copier le contenu d'un TabItem
    Bonjour

    je souhaite créer un Tabcontrol avec plusieurs onglets. Au départ il n'y a qu'un seul onglet et en cliquant sur "+" on copie l'onglet existant. Jusque là ça va :

    code xaml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <TabControl Name="tabDynamic" ItemsSource="{Binding}" SelectionChanged="tabDynamic_SelectionChanged" Margin="0,10,0,0">
                <TabControl.Resources>
                    <DataTemplate x:Key="TabHeader" DataType="TabItem">
                        <DockPanel>
                            <Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},Path=Name}">
                            </Button>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},Path=Header}"/>
                        </DockPanel>
                    </DataTemplate>
                </TabControl.Resources>         
            </TabControl>
    Code c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
     
    private List<TabItem> _tabItems;
            private TabItem _tabAdd;
     
            public MainWindow()
            {
                try
                {
                    InitializeComponent();
     
                    _tabItems = new List<TabItem>();
     
                    TabItem tabAdd = new TabItem();
                    tabAdd.Header = "+";
     
                    _tabItems.Add(tabAdd);
     
                    this.AddTabItem();
     
                    tabDynamic.DataContext = _tabItems;
                    tabDynamic.SelectedIndex = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
     
            }
     
            private TabItem AddTabItem()
            {
                int count = _tabItems.Count;
     
                TabItem tab = new TabItem();
                tab.Header = string.Format("Tab {0}", count);
                tab.Name = string.Format("Tab{0}", count);
                tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
     
                TextBox txt = new TextBox();
                txt.Name = "txt";
                tab.Content = txt;
     
     
     
                _tabItems.Insert(count - 1, tab);
                return tab;
            }
     
            private void tabDynamic_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                TabItem tab = tabDynamic.SelectedItem as TabItem;
                if (tab != null && tab.Header != null)
                {
                    if (tab.Header.Equals("+"))
                    {
                        tabDynamic.DataContext = null;
                        TabItem newTab = this.AddTabItem();
                        tabDynamic.DataContext = _tabItems;
                        tabDynamic.SelectedItem = newTab;
                    }
                    else
                    {
     
                    }
                }
            }
     
            private void btnDelete_Click(object sender, RoutedEventArgs e)
            {
                string tabName = (sender as Button).CommandParameter.ToString();
                var item = tabDynamic.Items.Cast<TabItem>().Where(i => i.Name.Equals(tabName)).SingleOrDefault();
     
                TabItem tab = item as TabItem;
                if (tab != null)
                {
                    if (_tabItems.Count<3)
                    {
                        MessageBox.Show("cannot remove last tab");
                    }
                    else if (MessageBox.Show(string.Format("Are you sure you want to remove the tab '{0}'?", tab.Header.ToString()),"remove Tab",MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        TabItem selectedTab = tabDynamic.SelectedItem as TabItem;
                        tabDynamic.DataContext = null;
                        _tabItems.Remove(tab);
                        tabDynamic.DataContext = _tabItems;
                        if (selectedTab == null || selectedTab.Equals(tab))
                        {
                            selectedTab = _tabItems[0];
                        }
                        tabDynamic.SelectedItem = selectedTab;
                    }
                }
            }
    Source du code : http://www.codeproject.com/Articles/...mically-in-WPF

    Maintenant je souhaite que le contenu du premier TabItem (checkbox pour l'exemple) soit également copié, pour cela je rajoute un DataTemplate :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <DataTemplate x:Key="TabHeader" DataType="TabItem">
                        <DockPanel>
                            <Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},Path=Name}">
                            </Button>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},Path=Header}"/>
                        </DockPanel>
                    </DataTemplate>
                    <DataTemplate x:Key="TabContent" DataType="TabItem">
                        <DockPanel>
                            <CheckBox Name="chk"></CheckBox>
                        </DockPanel>
                    </DataTemplate>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    ...
    TabItem tab = new TabItem();
                tab.Header = string.Format("Tab {0}", count);
                tab.Name = string.Format("Tab{0}", count);
                tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
     
                tab.ContentTemplate = tabDynamic.FindResource("TabContent") as DataTemplate;
     
                TextBox txt = new TextBox();
                txt.Name = "txt";
                tab.Content = txt;
    ...
    Ca marche aussi ! mais toutes mes checkbox ont le même nom, ce qui fait que lorsque je check celle dans le itemTab1, la checkbox du itemtab2 est également cochée... Je souhaite incrémenter le nom de mes checkbox au moment de la copie pour qu'elles soient uniques.

    Comment faire ?

    Merci d'avance pour votre aide.

  2. #2
    Membre averti
    Homme Profil pro
    Technicien
    Inscrit en
    Juin 2014
    Messages
    19
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Juin 2014
    Messages : 19
    Par défaut
    J'ai fini par trouver !!

    Il faut passer par un UserControl et là, plus de problème !

    code xaml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <TabControl Name="tabDynamic" ItemsSource="{Binding}" SelectionChanged="tabDynamic_SelectionChanged" Margin="0,10,0,0">
                <TabControl.Resources>
                    <DataTemplate x:Key="TabHeader" DataType="TabItem">
                        <DockPanel>
                            <Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},Path=Name}">
                            </Button>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}},Path=Header}"/>
                        </DockPanel>
                    </DataTemplate>
                </TabControl.Resources>         
            </TabControl>
    code c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
     
    private List<TabItem> _tabItems;
            private TabItem _tabAdd;
     
            public MainWindow()
            {
                try
                {
                    InitializeComponent();
     
                    _tabItems = new List<TabItem>();
     
                    TabItem tabAdd = new TabItem();
                    tabAdd.Header = "+";
     
                    _tabItems.Add(tabAdd);
     
                    this.AddTabItem();
     
                    tabDynamic.DataContext = _tabItems;
                    tabDynamic.SelectedIndex = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
     
            }
     
            private TabItem AddTabItem()
            {
                int count = _tabItems.Count;
     
                TabItem tab = new TabItem();
                tab.Header = string.Format("Tab {0}", count);
                tab.Name = string.Format("Tab{0}", count);
                tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
     
                UserControl Usctrl = new UserControl();
                tab.Content = Usctrl;
     
     
                _tabItems.Insert(count - 1, tab);
                return tab;
            }
     
            private void tabDynamic_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                TabItem tab = tabDynamic.SelectedItem as TabItem;
                if (tab != null && tab.Header != null)
                {
                    if (tab.Header.Equals("+"))
                    {
                        tabDynamic.DataContext = null;
                        TabItem newTab = this.AddTabItem();
                        tabDynamic.DataContext = _tabItems;
                        tabDynamic.SelectedItem = newTab;
                    }
                    else
                    {
     
                    }
                }
            }
     
            private void btnDelete_Click(object sender, RoutedEventArgs e)
            {
                string tabName = (sender as Button).CommandParameter.ToString();
                var item = tabDynamic.Items.Cast<TabItem>().Where(i => i.Name.Equals(tabName)).SingleOrDefault();
     
                TabItem tab = item as TabItem;
                if (tab != null)
                {
                    if (_tabItems.Count<3)
                    {
                        MessageBox.Show("cannot remove last tab");
                    }
                    else if (MessageBox.Show(string.Format("Are you sure you want to remove the tab '{0}'?", tab.Header.ToString()),"remove Tab",MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        TabItem selectedTab = tabDynamic.SelectedItem as TabItem;
                        tabDynamic.DataContext = null;
                        _tabItems.Remove(tab);
                        tabDynamic.DataContext = _tabItems;
                        if (selectedTab == null || selectedTab.Equals(tab))
                        {
                            selectedTab = _tabItems[0];
                        }
                        tabDynamic.SelectedItem = selectedTab;
                    }
                }
            }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Copier le contenu d'un répertoire
    Par bianconeri dans le forum C++Builder
    Réponses: 5
    Dernier message: 30/12/2006, 13h00
  2. [d6][TRichEdit] copier le contenu (rtf) d'un richedit
    Par Redsky dans le forum Composants VCL
    Réponses: 9
    Dernier message: 17/12/2005, 17h47
  3. [MySQL]Copier le contenu d'une BD MySQL
    Par Kubiak62 dans le forum Administration
    Réponses: 7
    Dernier message: 30/11/2005, 10h47
  4. copier le contenu d'une page web dans un fichier texte
    Par wassila dans le forum C++Builder
    Réponses: 30
    Dernier message: 28/08/2005, 22h27
  5. Réponses: 2
    Dernier message: 16/07/2004, 09h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo