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

Silverlight Discussion :

DataForm et DataTemplate


Sujet :

Silverlight

  1. #1
    Membre éprouvé Avatar de anthyme
    Homme Profil pro
    Inscrit en
    Mars 2004
    Messages
    1 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2004
    Messages : 1 559
    Par défaut DataForm et DataTemplate
    Bonjour,

    J'essai de faire un DataForm dans un DataTemplate, mais cela ne fonctionne pas, je ne dois pas bien m'y prendre ...

    En gros ce code fonctionne bien :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <ContentControl Grid.Row="0">
        <df:DataForm CurrentItem="{Binding Current}" CommandButtonsVisibility="None">
            <StackPanel>
                <df:DataField>
                    <TextBox Text="{Binding Title, Mode=TwoWay}" />
                </df:DataField>
                <df:DataField>
                    <TextBox Text="{Binding Description, Mode=TwoWay}" MinHeight="200" MaxHeight="500" />
                </df:DataField>
            </StackPanel>
        </df:DataForm>
    </ContentControl>
    Mais pas celui ci (la dataform est vide) :

    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
    <ContentControl Grid.Row="0">
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <df:DataForm CurrentItem="{Binding DataContext.Current, RelativeSource={RelativeSource Self}}" CommandButtonsVisibility="None">
                    <StackPanel>
                        <df:DataField>
                            <TextBox Text="{Binding Title, Mode=TwoWay}" />
                        </df:DataField>
                        <df:DataField>
                            <TextBox Text="{Binding Description, Mode=TwoWay}" MinHeight="200" MaxHeight="500" />
                        </df:DataField>
                    </StackPanel>
                </df:DataForm>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>
    J'ai raté quelque chose ?

    Merci.

  2. #2
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Me semble que ca vient de ton binding RelativeSource Self prend comme source soit meme (donc ici ton DataForm).

    Tu peux au choix essayer avec TemplatedParent au lieu de Self (mais je pense pas que ca fonctionne) ou alors mettre un nom a ton ContentControl et utiliser ElementName.

  3. #3
    Membre éprouvé Avatar de anthyme
    Homme Profil pro
    Inscrit en
    Mars 2004
    Messages
    1 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2004
    Messages : 1 559
    Par défaut
    TemplatedParent ne fonctionne pas ...

    Et le but étant de déclarer ce datatemplate ailleurs que dans cette vue donc pas d'element name

    Dommage ca marchait bien quand j'utilisais l'EditTemplate de la DataForm (Cf mon autre post)

  4. #4
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    C'est bizarre que le DataContext ne soit pas repercuté sur le ContentTemplate.

  5. #5
    Membre éprouvé Avatar de anthyme
    Homme Profil pro
    Inscrit en
    Mars 2004
    Messages
    1 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2004
    Messages : 1 559
    Par défaut
    Bon j'ai réussi a faire un truc qui marche avec le elementname (il faut donc que mon DataTemplate connaisse le nom de là où il sera utilisé, tant pis) mais je me retrouve à nouveau avec le problème que j'avais auparavant ... ici

    Solution 1 (fonctionne parfaitement) :

    Dans la child window :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <df:DataForm CurrentItem="{Binding Current}" CommandButtonsVisibility="None" >
        <StackPanel>
            <df:DataField>
                <TextBox Text="{Binding Title, Mode=TwoWay}" />
            </df:DataField>
            <df:DataField>
                <TextBox Text="{Binding Description, Mode=TwoWay}" MinHeight="200" MaxHeight="500" />
            </df:DataField>
            <df:DataField>
                <ComboBox ItemsSource="{Binding DataContext.Categories, ElementName=LayoutRoot}" 
                                        DisplayMemberPath="Name" SelectedItem="{Binding Category, Mode=TwoWay}" />
            </df:DataField>
        </StackPanel>
    </df:DataForm>
    Solution 2 :

    Dans la child window :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <ContentControl x:Name="FormContainer" ContentTemplate="{Binding DataTemplate}" />
    Ailleurs dans l'application (resource d'une autre vue) :

    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
    <DataTemplate x:Key="EditForm">
        <Grid DataContext="{Binding DataContext, ElementName=LayoutRoot}">
            <df:DataForm CurrentItem="{Binding Current}" CommandButtonsVisibility="None" >
                <StackPanel>
                    <df:DataField>
                        <TextBox Text="{Binding Title, Mode=TwoWay}" />
                    </df:DataField>
                    <df:DataField>
                        <TextBox Text="{Binding Description, Mode=TwoWay}" MinHeight="200" MaxHeight="500" />
                    </df:DataField>
                    <df:DataField>
                            <ComboBox ItemsSource="{Binding Source={StaticResource CategorySource}}" 
                                        DisplayMemberPath="Name" 
                            SelectedItem="{Binding Category, Mode=TwoWay}" />
                    </df:DataField>
                </StackPanel>
            </df:DataForm>
        </Grid>
    </DataTemplate>
    Et la le bug de la Category qui se change sur de multiple éléments dans la grille revient ...

    Pour info le code behind de ma child window (la propriété Categories ne sert que pour le code statique):

    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
    public partial class Form : ChildWindow
    {
        public Form()
        {
            InitializeComponent();
            DataContext = this;
        }
     
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Current is IEditableObject)
            {
                (Current as IEditableObject).EndEdit();
            }
            Current = null;
            this.DialogResult = true;
        }
     
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (Current is IEditableObject)
            {
                (Current as IEditableObject).CancelEdit();
                (Current as IEditableObject).EndEdit();
            }
            Current = null;
            this.DialogResult = false;
        }
     
        public DataTemplate DataTemplate
        {
            get { return (DataTemplate)GetValue(DataTemplateProperty); }
            set { SetValue(DataTemplateProperty, value); }
        }
     
        public static readonly DependencyProperty DataTemplateProperty =
            DependencyProperty.Register("DataTemplate", typeof(DataTemplate), typeof(Form), new PropertyMetadata(null));
     
        public object Current
        {
            get { return (object)GetValue(CurrentProperty); }
            set { SetValue(CurrentProperty, value); }
        }
     
        public static readonly DependencyProperty CurrentProperty =
            DependencyProperty.Register("Current", typeof(object), typeof(Form), new PropertyMetadata(null, new PropertyChangedCallback(CurrentChanged)));
     
     
        private static void CurrentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue is IEditableObject)
            {
                (args.NewValue as IEditableObject).BeginEdit();
            }
     
            if (args.OldValue is IEditableObject)
            {
                (args.OldValue as IEditableObject).EndEdit();
            }
        }
     
        public ObservableCollection<Category> Categories
        {
            get { return (ObservableCollection<Category>)GetValue(CategoriesProperty); }
            set { SetValue(CategoriesProperty, value); }
        }
     
        // Using a DependencyProperty as the backing store for Categories.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CategoriesProperty =
            DependencyProperty.Register("Categories", typeof(ObservableCollection<Category>), typeof(Form), new PropertyMetadata(null));
    }
    Et l'instanciation de la child window :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Form form = new Form
                {
                    Categories = (DataContext as TodoListViewModel).Categories as ObservableCollection<Category>,
                    DataTemplate = Resources["EditForm"] as DataTemplate,
                    Current = ((FrameworkElement) sender).DataContext,
                };
        form.Show();
    }
    Le code est pas parfaitement clean bien sûr (c'est du test pour le moment).

    Je vais devenir fou je pense ...

Discussions similaires

  1. Construire un DataTemplate de dataform dans le code behind
    Par abbepierre94 dans le forum Silverlight
    Réponses: 9
    Dernier message: 11/08/2010, 10h28
  2. [WPF][C#] DataTemplate qui marche en WPF mais pas en C#
    Par azerty53 dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 07/03/2008, 11h35
  3. [WPF] ajouter dynamiquement un control dans un dataTemplate.
    Par maa dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 28/02/2008, 08h20
  4. [WPF] datatemplate acces aux controls
    Par MrCyprom dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 21/09/2007, 16h17
  5. [WPF] ControlTemplate - DataTemplate - Style ?
    Par PlaTyPuSs dans le forum Windows Presentation Foundation
    Réponses: 23
    Dernier message: 10/05/2007, 13h52

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