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

C# Discussion :

WPF utilisation d'un userControl d'un projet vers un autre


Sujet :

C#

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Février 2015
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Chargé d'affaire

    Informations forums :
    Inscription : Février 2015
    Messages : 26
    Points : 36
    Points
    36
    Par défaut WPF utilisation d'un userControl d'un projet vers un autre
    Bonjour,
    voici un cas simple (a priori) :
    dans une solution WPF j'ai deux projets : Store (main) et Components.
    Dans le projet Component j'ai un UserControl avec un label et un TextBox que je souhaite réutiliser plein de fois dans le projet Store. J'ai donc fait comme suit :

    1) Dans le projet Component :
    Création d'une classe avec les propriétés du UserControl:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class CtrTextBoxProperties : DependencyObject
    {
        public static readonly DependencyProperty TextBoxLabelProperty =
            DependencyProperty.Register("TextBoxLabel", typeof(string), typeof(CtrTextBox), new PropertyMetadata(null));
        public string TextBoxLabel
        {
            get => (string)GetValue(TextBoxLabelProperty); set => SetValue(TextBoxLabelProperty, value);
        }
    }
    Création du UserControl:
    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
    <UserControl
        x:Class="Components.CtrTextBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prop="clr-namespace:Components"
        d:DataContext="{d:DesignInstance Type=prop:CtrTextBoxProperties}"
        d:DesignHeight="50"
        d:DesignWidth="800"
        mc:Ignorable="d">
        <StackPanel Orientation="Horizontal">
            <Label Content="{Binding TextBoxLabel}" />
            <TextBox Width="300" Margin="5" />
        </StackPanel>
    </UserControl>
    2) Dans le projet Store
    ajout d'une référence au projet Component.
    Dans le MainWindow j'appelle le UserControl

    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
    <Window
        x:Class="Store.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ucs="clr-namespace:Components;assembly=Components"
        Title="MainWindow"
        Width="800"
        Height="450"
        mc:Ignorable="d">
        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ucs:CtrTextBox
                TextBoxLabel
        </Grid>
    </Window>
    Et voici une capture d'écran qui montre que la propriété TextBoxLabel n'est pas reconnue...

    Nom : Capture d’écran 2023-09-14 053131.png
Affichages : 79
Taille : 37,0 Ko

    Comment faire à votre avis ??

  2. #2
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    1 322
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 1 322
    Points : 1 928
    Points
    1 928
    Par défaut
    Sauf erreur de ma part, il faut avoir au moins une fois "générer" le composant pour qu'il soit utilisable et donc reconnu (en tout cas en projet winforms, donc je suppose que c'est pareil ici)

  3. #3
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 177
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 177
    Points : 25 125
    Points
    25 125
    Par défaut
    class CtrTextBoxProperties


    DependencyProperty.Register(.... , typeof(CtrTextBox

    ca semble être un problème de cohérence, tu as renommé la classe à la main ?

Discussions similaires

  1. Utilisation d'un usercontrol WPF (xaml) dans windev
    Par jurassic pork dans le forum Contribuez
    Réponses: 0
    Dernier message: 15/12/2020, 08h50
  2. [C#][WPF] Rendre un UserControl WPF utilisable depuis COM
    Par alavoler dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 27/10/2010, 16h23
  3. utiliser un seul window pour un projet wpf
    Par belfafi dans le forum Windows Presentation Foundation
    Réponses: 10
    Dernier message: 17/03/2010, 12h03
  4. utiliser le même fichier dans plusieurs projets vc++6
    Par yannick_sch dans le forum MFC
    Réponses: 5
    Dernier message: 12/02/2004, 17h39

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