Problème pour trace une ligne en WPF

Résolu
cs_sondos01 Messages postés 22 Date d'inscription mardi 10 juillet 2007 Statut Membre Dernière intervention 14 décembre 2012 - 3 juil. 2008 à 11:58
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 - 3 juil. 2008 à 15:53
 



----

<table align="left" cellpadding="0" cellspacing="0" width="90%">

----,
Bonjour,
Je travaille sur un projet WPF et j'essaye de trouver un moyen pour tracer une ligne sur ma fenêtre.

J'ai
trouvé le code correspondant en C# mais je ne sais pas comment je fais
pour l'intégrer dans mon fichier. le code est le suivant.

using System;
using System.Collections.Generic;
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.Shapes;

namespace CreateLine
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();

           
   
        }

        private void WindowLoaded(object sender, EventArgs e) {

            // Add a Line Element
            myLine = new Line();
            myLine.Stroke = Brushes.LightSteelBlue;
            myLine.X1 = 1;
            myLine.X2 = 50;
            myLine.Y1 = 1;
            myLine.Y2 = 50;
            myLine.HorizontalAlignment = HorizontalAlignment.Left;
            myLine.VerticalAlignment = VerticalAlignment.Center;
            myLine.StrokeThickness = 2;
            myGrid.Children.Add(myLine);
        }
     
 
 
       
       
      

    }
}

L'erreur qui s'affiche est la suivante:
The name my Line does not exist in the current context.

Je travaille sous VS 2005 .Net 3.0

Mon code XAML correspondant à cette partie est le suivant :

<Window x:Class=\"CreateLine.Window1\"
    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
    xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
    Title=\"CreateLine\" Height=\"300\" Width=\"300\"
    Loaded=\"WindowLoaded\"
    >
    <Grid>
       
    </Grid>
</Window>

Merci pour votre aide

Sondos

</td>

</td>
</tr>
<tr>
<td align="left" valign="bottom" width="100%">


</td>
</tr>
</tbody>
</table>

4 réponses

krishou Messages postés 60 Date d'inscription dimanche 9 janvier 2005 Statut Membre Dernière intervention 21 août 2008 1
3 juil. 2008 à 15:26
Je ne m'y connais pas en WPF mais il me semble que ton "myLine" n'est pas déclaré.... Remplace le "myLine = new Line();" par "Line my Line= new Line();",
ca devrait fonctionner
3
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
3 juil. 2008 à 15:27
Salut,

ta variable myLine n'est pas déclarée :

Line myLine = new Line()

Mx
MVP C# 
3
cs_sondos01 Messages postés 22 Date d'inscription mardi 10 juillet 2007 Statut Membre Dernière intervention 14 décembre 2012
3 juil. 2008 à 15:41
Effectivement,
il manquer de l'instancier.
Il ne fallait pas aussi ajouter:

   Grid myGrid= new Grid();
   this.Content = myGrid;

Merci à tous

 
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
3 juil. 2008 à 15:53
Ou alors, dans ton code XAML, a la définition de ta grid

<Grid x:Name="myGrid">
</Grid>

Mx
MVP C# 
0
Rejoignez-nous