Bonour,

J'ai quelque difficulté à comprendre exactemement le fonctionnement des userControl sur un point:

Je souhaite créer un control (qui ce rapproche d'une listebox) et souhaite donc ajouter des items a celui ci (coté XAML).

Voici le code de mon control
XAML
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
<UserControl x:Class="Test.UCBarreAction.ChoixAction"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="Auto" Height="23" >
 
<Grid x:Name="LayoutRoot" Background="{x:Null}" Height="23">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="24"/>
</Grid.ColumnDefinitions>
 
<Rectangle HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="rctFond" Width="23" Fill="#FFE9EEEE" Stroke="{x:Null}" StrokeThickness="0"/>
<Rectangle Margin="0,0,0,0" Fill="#FFC5C5C5" Stroke="{x:Null}" StrokeThickness="0" HorizontalAlignment="Right" x:Name="rctBordure" Width="1"/>
 
<StackPanel Grid.ColumnSpan="3"  x:Name="stkChoix"/>
 
</Grid>
Le CS
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
namespace Test.UCBarreAction
{
    [TemplatePart(Name="ListeChoix", Type=typeof(StackPanel))]
    public partial class ChoixAction : UserControl
    {
 
         public ChoixAction()
        {
            InitializeComponent();
 
            //Bon, cette ligne ne marche pas comme vous pouvez vous en douter  mais j'ai testé des trucs
            stkChoix = GetTemplateChild("ListeChoix") as StackPanel;
        }
    }
}
Et voila le XAML de ma page ou j'utilise mon control (sachant que les ItemChoix sont également des UserControl)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
<UCBarreAction:ChoixAction >
  <UCBarreAction:ChoixAction.ListeChoix>
    <UCBarreAction:ItemChoix Text="Actions" Valide="True"/>
    <UCBarreAction:ItemChoix Text="Résumé" Valide="True"/>
  </UCBarreAction:ChoixAction.ListeChoix>
</UCBarreAction:ChoixAction>
Voilà, le but est que les ItemChoix que je déclare comme enfant de L'userControl ChoixAction se retrouve dans son StackPanel stkChoix. j'espère que je suis clair.

Je me doute bien que je m'y suis pas pris de la bonne manière mais je ne trouve pas de tutoriel clair sur le net à propos de ce point.

Donc si vous savez comment faire ou que vous connaisser un site qui l'explique merci de me répondre.