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 :

Visibilité des champs XAML d'une UserControl déclaré dans une DLL


Sujet :

Windows Presentation Foundation

  1. #1
    Membre du Club Avatar de Lelio1407
    Inscrit en
    Juillet 2009
    Messages
    126
    Détails du profil
    Informations forums :
    Inscription : Juillet 2009
    Messages : 126
    Points : 68
    Points
    68
    Par défaut Visibilité des champs XAML d'une UserControl déclaré dans une DLL
    Bonjour à tous,

    J'ai une solution composée de 2 projets :
    - l'application principale,
    - une dll.

    Dans la Dll, j'ai crée un UserControl WPF, appelé ScreenBaseInfos et qui contient (entre autres) une ListBox déclarée ainsi :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    <ListBox Name="ScreenBaseInfos_ListBox"  ItemsSource="{Binding}" />

    Dans mon application principale, le UserControl est déclaré de façon dynamique ainsi :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    xmlns:dll="clr-namespace:MyDLL;assembly=MyDLL"
    <dll:ScreenBaseInfos Name="ScreenBaseInfos_UserControl" >
    </dll:ScreenBaseInfos>

    Dans l'application principale, je crée une ObservableCollection<GraphicObject> appelée graphicObjectsCollection et je souhaite alors "binder" la ScreenBaseInfos_ListBox de mon ScreenBaseInfos_UserControl avec cette graphicObjectsCollection.

    Si le UserControl est déclaré dans l'application principale, le champ "ScreenBaseInfos_ListBox" est visible. Si le UserControl est déclaré dans ma dll, il ne l'est plus.

    Ma question est donc la suivante : comment peut-on accéder aux champs "XAML" (ici, ma ScreenBaseInfos_ListBox) d'un UserControl défini dans une DLL depuis l'application principale?

    Je vous remercie d'avance pour votre aide.

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour Lelio1407
    Si c'est pour personnaliser l'aspect du control on n'a pas besoin à priori.
    Tout se fait dans le usercontrol en xaml.
    Si c'est pour le binding aux "data" il faut :
    -creer 2 dependency property ItemsSource & ItemTemplate pour usercontrol qui en est depourvu .Elles permettre de faire le "pont" vers ses "nested" ItemsControl.
    -pour les controles autres que ItemControl il faut creer une dependency property pour chaque prop à exposer & pour chacun des controles (Text1 pour TextBlock1,Source1 pour Image1 ,Text2 pour TextBlock2,Source2 pour Image2 etc...)...
    voici un bout de code.
    code behind usercontrol:
    Code c# : 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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    //nedeed
    using System.Collections;
     
    namespace WpfLibrary
    {
        /// <summary>
        /// Logique d'interaction pour UserControlBogus.xaml
        /// </summary>
        public partial class UserControlBogus : UserControl
        {
            public UserControlBogus()
            {
                InitializeComponent();
            }
            //Ajout de la prop ItemsSource à UserControl
            //devient owner de cette prop comme un  ItemsControl
            public static readonly DependencyProperty ItemsSourceProperty =
     ItemsControl.ItemsSourceProperty.AddOwner(typeof(UserControlBogus));
     
            public IEnumerable ItemsSource
            {
                get
                {
                    return (IEnumerable)GetValue(ItemsSourceProperty);
                }
                set { SetValue(ItemsSourceProperty, value); }
            }
            //Ajout de la prop ItemTemplate à UserControl
            //devient owner de cette prop comme un  ItemsControl
            public static readonly DependencyProperty ItemTemplateProperty =
             ItemsControl.ItemTemplateProperty.AddOwner(typeof(UserControlBogus));
            public DataTemplate ItemTemplate
            {
                get
                {
                    return (DataTemplate)GetValue(ItemTemplateProperty);
                }
                set { SetValue(ItemTemplateProperty, value); }
            }
            //Ajout de la prop Txt à UserControl
            //pour TextBlock
            public static DependencyProperty TxtProperty =
                DependencyProperty.Register("Txt", typeof(string), typeof(UserControlBogus));
            public string Txt
            {
                get { 
                    return (string)GetValue(TxtProperty); 
                }
                set 
                { 
                    SetValue(TxtProperty, value); }
            }
     
        }
    }
    code xaml usercontrol:
    Code xaml : 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
     
    <UserControl x:Class="WpfLibrary.UserControlBogus"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="MyControl">
        <Grid
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            MinHeight="300"
            MinWidth="300">
            <Grid.RowDefinitions>
                <RowDefinition  Height="30"></RowDefinition>
                <RowDefinition Height="*" ></RowDefinition>
                <RowDefinition Height="60"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock 
                FontSize="18"
                Background="Gold"
                Foreground="Black"
                HorizontalAlignment="Stretch"
                TextAlignment="Center"
                FontWeight="Bold"
                Text="{Binding ElementName=MyControl, Path=Txt}" /> 
            <ListBox 
                Grid.Row="1"
                x:Name="Listbox1" 
                HorizontalAlignment="Stretch"
                Background="Chartreuse"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding ElementName=MyControl, Path=ItemsSource}" 
                ItemTemplate="{Binding ElementName=MyControl, Path=ItemTemplate}" > 
            </ListBox>
            <ComboBox 
                Grid.Row="2"
                x:Name="Combo" 
                Background="BlanchedAlmond"
                FontSize="14"
                Foreground="RoyalBlue"
                HorizontalAlignment="Stretch"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding ElementName=MyControl, Path=ItemsSource}" 
                ItemTemplate="{Binding ElementName=MyControl, Path=ItemTemplate}" > 
            </ComboBox >
            <ListBox 
                Grid.Row="3"
                x:Name="Listbox2" 
                Background="Lavender"
                FontSize="14"
                Foreground="Black"
                HorizontalAlignment="Stretch"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding ElementName=MyControl, Path=ItemsSource}" 
                ItemTemplate="{Binding ElementName=MyControl, Path=ItemTemplate}" > 
            </ListBox>
        </Grid>
    </UserControl>
    code behind winform:
    Code c# : 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
    94
    95
    96
    97
    98
    99
    100
    101
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
     
    namespace WpfTestUserControlBogus
    {
        /// <summary>
        /// Logique d'interaction pour Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                Window1 mainWindow = (Window1)Application.Current.MainWindow;
     
            }
        }
     
        //Classes Data
     
        public class monItem : INotifyPropertyChanged
        {
            private string _Nom;
            public string Nom
            {
                get { return _Nom; }
                set { _Nom = value; InotifyPropertyChanged("Nom"); }
     
            }
            private string _Prenom;
            public string Prenom
            {
                get { return _Prenom; }
                set { _Prenom = value; InotifyPropertyChanged("Prenom"); }
            }
            private bool _CanAnimated;
            public bool CanAnimated
            {
                get { return _CanAnimated; }
                set { _CanAnimated = value; InotifyPropertyChanged("CanAnimated"); }
            }
     
     
            #region INotifyPropertyChanged Membres
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void InotifyPropertyChanged(String nameProp)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameProp));
                }
            }
     
            #endregion
        }
     
        public class Items : ObservableCollection<monItem>
        {
            private monItem o;
            public Items()
            {
                Random rnd = new Random();
                for (int i = 0; i < 19; i++)
                {
                    o = new monItem();
                    o.Nom = "Item" + (i + 1).ToString();
                    o.Prenom = "CetItem" + (i + 1).ToString();
                    int nb = rnd.Next(1, 21);
     
                    switch (nb)
                    {
                        case 0 - 7:
                            o.CanAnimated = true;
                            break;
                        case 8 - 14:
                            o.CanAnimated = false;
                            break;
                        default:
                            o.CanAnimated = true;
                            break;
                    }
                    this.Add(o);
                }
            }
        }
    }

    code xaml winform:
    Code xaml : 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
     
    <Window x:Class="WpfTestUserControlBogus.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTestUserControlBogus"
        xmlns:lib="clr-namespace:WpfLibrary;assembly=WpfLibrary"
        Title="Window2" Height="370" Width="380" >
        <Window.Resources>
            <local:Items x:Key="mySource"></local:Items>
     
            <DataTemplate x:Key="monItemsTemplate" 
                          DataType="{x:Type local:monItem}">
                <WrapPanel>
                    <TextBlock Text="{Binding Path=Nom}"  Margin="10,10,10,10"/>
                     <TextBlock Text="{Binding Path=Prenom}" Margin="10,10,10,10"/>
                     <TextBlock Text="{Binding Path=CanAnimated}" Margin="10,10,10,10"/>
                </WrapPanel>
            </DataTemplate>
     
        </Window.Resources>
        <Grid     
            Margin="10,10,10,10" > 
           <Grid.RowDefinitions>
                <RowDefinition MinHeight="60"></RowDefinition>
            </Grid.RowDefinitions>
            <lib:UserControlBogus 
                 Margin="0,0,0,0"
                 Name="Bogus"
                 Txt="{Binding Source={StaticResource mySource},Path=Nom}" 
                 ItemsSource ="{Binding Source={StaticResource mySource}}"              
                 ItemTemplate="{StaticResource monItemsTemplate}" 
                                  > 
            </lib:UserControlBogus>
        </Grid>
    </Window>
    bon code................

  3. #3
    Membre éprouvé Avatar de jmix90
    Homme Profil pro
    Consultant .Net
    Inscrit en
    Juillet 2007
    Messages
    576
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Consultant .Net
    Secteur : Conseil

    Informations forums :
    Inscription : Juillet 2007
    Messages : 576
    Points : 998
    Points
    998
    Par défaut
    Cela n'aurait pas suffit cela ? :

    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     
    <dll:ScreenBaseInfos xmlns:dll="clr-namespace:MyDLL;assembly=MyDLL" x:Name="ScreenBaseInfos_UserControl" DataContext="{Binding graphicObjectsCollection }">
    </dll:ScreenBaseInfos>

    Par contre dans l'absolu, un UserControl reste assez spécifique à un besoin métier et son rarement placés dans une Dll à part.

    Bon code aussi ;-)
    Jonathan ANTOINE - Découvrez mon livre: MVVM, de la découverte à la maîtrise.

    Microsoft MVP Client Application Development
    - MCPD Windows 4.0, etc.
    Mon blog : http://www.jonathanantoine.com

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

Discussions similaires

  1. Réponses: 82
    Dernier message: 05/02/2011, 14h34
  2. Réponses: 6
    Dernier message: 13/11/2009, 16h06
  3. Réponses: 0
    Dernier message: 09/09/2008, 21h53
  4. Réponses: 1
    Dernier message: 11/06/2008, 13h33
  5. Réponses: 27
    Dernier message: 12/04/2007, 10h23

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