[debutant] Probleme de duplicate name pendant deserialisation Wpf

dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021 - 18 oct. 2019 à 14:30
Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 - 6 nov. 2019 à 12:54
Bonjour, J'ai créè un usercontrol constituer de Label, Textbox, Grid, Combobox.
L'utilisateur peut en ajouter plusieurs Dynamiquement.
Quand je les sérialise via:
 private void SaveBlockBtn_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder outstr = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            settings.CheckCharacters = false;
            settings.NewLineOnAttributes = true;
           
            XamlDesignerSerializationManager dsm = new   XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
            dsm.XamlWriterMode = XamlWriterMode.Expression;
            XamlWriter.Save(PatchStackPnl, dsm);
            string savedControls = outstr.ToString();
            File.WriteAllText(@"C:\Users\Desktop\Lighting Tools\MyDesign.Xml", savedControls);
           
        }

j’obtiens Bien mon Fichier Xml "Mydesign" Avec toutes les info dedans.
En revanche Quant je veux le désérialiser j'ai un message d'erreur:
Impossible d'inscrire le nom dupliqué dans cette portée. En gros il me dit que j'utilise plusieurs fois le même nom de control.
Dans le fichier Xaml du User control je donne de X:name a chaque control pour pouvoir les appeler via le code Behind.
En cherchant a droite a gauche, j'ai trouvé quelques infos autour Des x:key(que j'utilise pour différent style).Mais je ne sais pas comment m'en servir pour le nom d'un control.


Merci d'avance pour votre aide

9 réponses

Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 624
18 oct. 2019 à 17:44
Bonsoir, sans le xaml, et au moins un extrait du fichier xml, impossible de te répondre
0
dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021
Modifié le 21 oct. 2019 à 19:24
Merci Whismeril,
désolé pour la réponse tardive mais en ce moment j'ai énormément de boulot et malheureusement très peu de temps pour moi.

Une Partie xaml du userControl (pas tout car il est juste énorme)


<UserControl x:Name="AllBlock"  x:Class="Lighting_Tools.Block" 
             
             xmlns:local="clr-namespace:Lighting_Tools"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"  Height="318" Width="760" AllowDrop="True" DragEnter="AllBlock_DragEnter" MouseDown="AllBlock_MouseDown" MouseDoubleClick="AllBlock_MouseDoubleClick" MouseEnter="AllBlock_MouseEnter" MouseLeave="AllBlock_MouseLeave">

    <UserControl.Resources >
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Resources/UserControl/BlockControl.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid Name="AllBlockContent" Background="White" Margin="0,0,-1.2,-0.2" Cursor="Hand"  >

        <GroupBox Header="Malighting" x:Name="BlockBox"   HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="1" Background="LightBlue" Margin="4,-1,0,0" Height="208" >

            <Grid  x:Name="BlockGrid" HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" Width="556" Background="LightGray" Height="173" >
                <ComboBox x:Name="Circuit1Cbx" Grid.Column="0" Grid.ColumnSpan="2" SelectionChanged="Circuit1Cbx_SelectionChanged" ItemsSource="{Binding}" />
                <ComboBox x:Name="Circuit2Cbx" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" SelectionChanged="Circuit2Cbx_SelectionChanged"/>
                <Label x:Name="Ph1albl" Grid.Column="0" Grid.Row="2" Content="PH1" Style="{StaticResource Ph1label}"/>
                <Label x:Name="Ph2albl" Grid.Column="0" Grid.Row="3" Content="PH2" Style="{StaticResource Ph2label}"/>
                <Label x:Name="Ph3albl" Grid.Column="0" Grid.Row="4" Content="PH3" Style="{StaticResource Ph3label}"/>
                <Label x:Name="Ph1blbl" Grid.Column="0" Grid.Row="5" Content="PH1" Style="{StaticResource Ph1label}"/>
                <Label x:Name="Ph2blbl" Grid.Column="0" Grid.Row="6" Content="PH2" Style="{StaticResource Ph2label}"/>
                <Label x:Name="Ph3blbl" Grid.Column="0" Grid.Row="7" Content="PH3" Style="{StaticResource Ph3label}"/>
                <Label x:Name="Ph1clbl" Grid.Column="0" Grid.Row="8" Content="PH1" Style="{StaticResource Ph1label}"/>
                <Label x:Name="Ph2clbl" Grid.Column="0" Grid.Row="9" Content="PH2" Style="{StaticResource Ph2label}"/>
                <Label x:Name="Ph3clbl" Grid.Column="0" Grid.Row="10" Content="PH3" Style="{StaticResource Ph3label}"/>
                <Label x:Name="Ph1dlbl" Grid.Column="0" Grid.Row="11" Content="PH1" Style="{StaticResource Ph1label}"/>
                <Label x:Name="Ph2dlbl" Grid.Column="0" Grid.Row="12" Content="PH2" Style="{StaticResource Ph2label}"/>
                <Label x:Name="Ph3dlbl" Grid.Column="0" Grid.Row="13" Content="PH3" Style="{StaticResource Ph3label}"/>
                <Label x:Name="Spot1aLbl" Grid.Column="1" Grid.Row="2"  Style="{StaticResource BlockLabel}" />
                <Label x:Name="Spot1bLbl" Grid.Column="2" Grid.Row="2" Style="{StaticResource BlockLabel}" />
                <Label x:Name="Spot1cLbl" Grid.Column="3" Grid.Row="2" Style="{StaticResource BlockLabel}" />
                <Label x:Name="Spot1dLbl" Grid.Column="4" Grid.Row="2" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot1eLbl" Grid.Column="5" Grid.Row="2" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot1fLbl" Grid.Column="6" Grid.Row="2" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot1gLbl" Grid.Column="7" Grid.Row="2" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot1hLbl" Grid.Column="8" Grid.Row="2" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2aLbl" Grid.Column="1" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2bLbl" Grid.Column="2" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2cLbl" Grid.Column="3" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2dLbl" Grid.Column="4" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2eLbl" Grid.Column="5" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2fLbl" Grid.Column="6" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2gLbl" Grid.Column="7" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot2hLbl" Grid.Column="8" Grid.Row="3" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3aLbl" Grid.Column="1" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3bLbl" Grid.Column="2" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3cLbl" Grid.Column="3" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3dLbl" Grid.Column="4" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3eLbl" Grid.Column="5" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3fLbl" Grid.Column="6" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3gLbl" Grid.Column="7" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot3hLbl" Grid.Column="8" Grid.Row="4" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4aLbl" Grid.Column="1" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4bLbl" Grid.Column="2" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4cLbl" Grid.Column="3" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4dLbl" Grid.Column="4" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4eLbl" Grid.Column="5" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4fLbl" Grid.Column="6" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4gLbl" Grid.Column="7" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot4hLbl" Grid.Column="8" Grid.Row="5" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot5aLbl" Grid.Column="1" Grid.Row="6" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot5bLbl" Grid.Column="2" Grid.Row="6" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot5cLbl" Grid.Column="3" Grid.Row="6" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot5dLbl" Grid.Column="4" Grid.Row="6" Style="{StaticResource BlockLabel}"/>
                <Label x:Name="Spot5eLbl" Grid.Column="5" Grid.Row="6" Style="{StaticResource BlockLabel}"/>


Et voici egalement unepartie du Xml:
<?xml version="1.0"?>

-<StackPanel xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:lt="clr-namespace:Lighting_Tools;assembly=Lighting Tools" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" VerticalAlignment="Top" HorizontalAlignment="Left" Height="Auto" Width="Auto" Name="PatchStackPnl" Orientation="Vertical">


-<lt:Block Height="225" Width="760" Name="AllBlock0" AllowDrop="True" FontSize="8" FontFamily="Arial">


-<Grid Name="AllBlockContent" Cursor="Hand" Margin="0,0,-1.2,-0.2" Background="#FFFFFFFF">


-<GroupBox VerticalAlignment="Top" HorizontalAlignment="Left" Height="121" Name="BlockBox" Margin="4,-1,0,0" Foreground="#FF000000" BorderThickness="1,1,1,1" BorderBrush="#FF000000" Header="Direct">


-<GroupBox.Background>


-<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">


-<LinearGradientBrush.GradientStops>

<GradientStop Offset="0" Color="#FFFFA500"/>

<GradientStop Offset="1" Color="#FFFFFFFF"/>

</LinearGradientBrush.GradientStops>

</LinearGradientBrush>

</GroupBox.Background>


-<Grid VerticalAlignment="Top" HorizontalAlignment="Left" Height="123" Width="556" Name="BlockGrid" Margin="10,5,0,0" Background="#FFD3D3D3">


-<Grid.ColumnDefinitions>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

<ColumnDefinition/>

</Grid.ColumnDefinitions>


-<Grid.RowDefinitions>

<RowDefinition Height="Auto" Name="Title"/>

<RowDefinition Height="Auto" Name="Ligne0"/>

<RowDefinition Height="Auto" Name="Ligne1"/>

<RowDefinition Height="Auto" Name="Ligne2"/>

<RowDefinition Height="Auto" Name="Ligne3"/>

<RowDefinition Height="Auto" Name="Ligne4"/>

<RowDefinition Height="Auto" Name="Ligne5"/>

<RowDefinition Height="Auto" Name="Ligne6"/>

<RowDefinition Height="0" Name="Ligne7"/>

<RowDefinition Height="0" Name="Ligne8"/>

<RowDefinition Height="0" Name="Ligne9"/>

<RowDefinition Height="0" Name="Ligne10"/>

<RowDefinition Height="0" Name="Ligne11"/>

<RowDefinition Height="0" Name="Ligne12"/>

</Grid.RowDefinitions>


-<ComboBox Name="Circuit1Cbx" xml:space="preserve" Grid.ColumnSpan="2" Grid.Column="0">

<s:String>C1.1. </s:String>

<s:String>C1.2. </s:String>

<s:String>C1.3. </s:String>

<s:String>C1.4. </s:String>

<s:String>C2.1. </s:String>

<s:String>C2.2. </s:String>

<s:String>C2.3. </s:String>

<s:String>C2.4. </s:String>

<s:String>C2.5. </s:String>

<s:String>C2.6. </s:String>

<s:String>C2.7. </s:String>

<s:String>C2.8. </s:String>

<s:String>C3.1. </s:String>

<s:String>C3.2. </s:String>

<s:String>C3.3. </s:String>

<s:String>C3.4. </s:String>

<s:String>C3.5. </s:String>

<s:String>C3.6. </s:String>

<s:String>C3.7. </s:String>

<s:String>C3.8. </s:String>

<s:String>C3.9. </s:String>

<s:String>C4.1. </s:String>

<s:String>C4.2. </s:String>

<s:String>C4.3. </s:String>

<s:String>C4.4. </s:String>

<s:String>C4.5. </s:String>

<s:String>C4.6. </s:String>

<s:String>C4.7. </s:String>

<s:String>C4.8. </s:String>

<s:String>C5.1. </s:String>

<s:String>C5.2. </s:String>

<s:String>C5.3. </s:String>

<s:String>C5.4. </s:String>

<s:String>CAC1. </s:String>

<s:String>CAC2. </s:String>

<s:String>CAC3. </s:String>

<s:String>CAJ1. </s:String>

<s:String>CAJ2. </s:String>

<s:String>CAJ3. </s:String>

<s:String>ECR1. </s:String>

<s:String>ECR2. </s:String>

<s:String>ECR3. </s:String>

<s:String>F1. </s:String>

<s:String>F10. </s:String>

<s:String>F11. </s:String>

<s:String>F12. </s:String>

<s:String>F13. </s:String>

<s:String>F2. </s:String>

<s:String>F3. </s:String>

<s:String>F4. </s:String>

<s:String>F5. </s:String>

<s:String>F6. </s:String>

<s:String>F7. </s:String>

<s:String>F8. </s:String>

<s:String>F9. </s:String>

<s:String>S1. </s:String>

<s:String>S2. </s:String>

<s:String>SOL1. </s:String>

<s:String>SOL2. </s:String>

<s:String>SOL3. </s:String>

<s:String>STA1. </s:String>

<s:String>STA2. </s:String>

<s:String>STA3. </s:String>

<s:String>STA4. </s:String>

<s:String>STA5. </s:String>

</ComboBox>


-<ComboBox Name="Circuit2Cbx" xml:space="preserve" Grid.ColumnSpan="2" Grid.Column="0" Grid.Row="1" Visibility="Hidden">

<s:String>C1.1. </s:String>

<s:String>C1.2. </s:String>

<s:String>C1.3. </s:String>

<s:String>C1.4. </s:String>

<s:String>C2.1. </s:String>

<s:String>C2.2. </s:String>

<s:String>C2.3. </s:String>

<s:String>C2.4. </s:String>

<s:String>C2.5. </s:String>

<s:String>C2.6. </s:String>

<s:String>C2.7. </s:String>

<s:String>C2.8. </s:String>

<s:String>C3.1. </s:String>

<s:String>C3.2. </s:String>

<s:String>C3.3. </s:String>

<s:String>C3.4. </s:String>

<s:String>C3.5. </s:String>

<s:String>C3.6. </s:String>

<s:String>C3.7. </s:String>

<s:String>C3.8. </s:String>

<s:String>C3.9. </s:String>

<s:String>C4.1. </s:String>

<s:String>C4.2. </s:String>

<s:String>C4.3. </s:String>

<s:String>C4.4. </s:String>

<s:String>C4.5. </s:String>

<s:String>C4.6. </s:String>

<s:String>C4.7. </s:String>

<s:String>C4.8. </s:String>

<s:String>C5.1. </s:String>

<s:String>C5.2. </s:String>

<s:String>C5.3. </s:String>

<s:String>C5.4. </s:String>

<s:String>CAC1. </s:String>

<s:String>CAC2. </s:String>

<s:String>CAC3. </s:String>

<s:String>CAJ1. </s:String>

<s:String>CAJ2. </s:String>

<s:String>CAJ3. </s:String>

<s:String>ECR1. </s:String>

<s:String>ECR2. </s:String>

<s:String>ECR3. </s:String>

<s:String>F1. </s:String>

<s:String>F10. </s:String>

<s:String>F11. </s:String>

<s:String>F12. </s:String>

<s:String>F13. </s:String>

<s:String>F2. </s:String>

<s:String>F3. </s:String>

<s:String>F4. </s:String>

<s:String>F5. </s:String>

<s:String>F6. </s:String>

<s:String>F7. </s:String>

<s:String>F8. </s:String>

<s:String>F9. </s:String>

<s:String>S1. </s:String>

<s:String>S2. </s:String>

<s:String>SOL1. </s:String>

<s:String>SOL2. </s:String>

<s:String>SOL3. </s:String>

<s:String>STA1. </s:String>

<s:String>STA2. </s:String>

<s:String>STA3. </s:String>

<s:String>STA4. </s:String>

<s:String>STA5. </s:String>

</ComboBox>


-<Label Name="Ph1albl" Background="#FFFF0000" Grid.Column="0" Grid.Row="2">


-<Label.Style>


-<Style TargetType="Label">


-<Style.Resources>

<ResourceDictionary/>

</Style.Resources>


-<Setter Property="Control.Padding">


-<Setter.Value>

<Thickness>0,0,0,0</Thickness>

</Setter.Value>

</Setter>


-<Setter Property="Panel.Background">


-<Setter.Value>

<SolidColorBrush>#FFFF0000</SolidColorBrush>

</Setter.Value>

</Setter>


-<Setter Property="ContentControl.Content">

<Setter.Value>0</Setter.Value>

</Setter>


-<Setter Property="TextElement.FontSize">


-<Setter.Value>

<s:Double>8</s:Double>

</Setter.Value>

</Setter>


-<Setter Property="Control.HorizontalContentAlignment">


-<Setter.Value>

<x:Static Member="HorizontalAlignment.Center"/>

</Setter.Value>

</Setter>


-<Setter Property="Control.VerticalContentAlignment">


-<Setter.Value>

<x:Static Member="VerticalAlignment.Center"/>

</Setter.Value>

</Setter>


-<Setter Property="Border.BorderBrush">


-<Setter.Value>

<SolidColorBrush>#00FFFFFF</SolidColorBrush>

</Setter.Value>

</Setter>


-<Setter Property="Border.BorderThickness">


-<Setter.Value>

<Thickness>1,1,1,1</Thickness>

</Setter.Value>

</Setter>


-<Setter Property="TextElement.FontWeight">


-<Setter.Value>

<FontWeight>Black</FontWeight>

</Setter.Value>

</Setter>

</Style>

</Label.Style>
Ph1
</Label>


-<Label Name="Ph2albl" Background="#FFFFFF00" Grid.Column="0" Grid.Row="3">


-<Label.Style>


-<Style TargetType="Label">


-<Style.Resources>

<ResourceDictionary/>

</Style.Resources>


-<Setter Property="Control.Padding">


-<Setter.Value>

<Thickness>0,0,0,0</Thickness>

</Setter.Value>

</Setter>


-<Setter Property="Panel.Background">


-<Setter.Value>

<SolidColorBrush>#FFFFFF00</SolidColorBrush>

</Setter.Value>

</Setter>


-<Setter Property="ContentControl.Content">

<Setter.Value>0</Setter.Value>

</Setter>


-<Setter Property="TextElement.FontSize">


-<Setter.Value>

<s:Double>8</s:Double>

</Setter.Value>

</Setter>


-<Setter Property="Control.HorizontalContentAlignment">


-<Setter.Value>

<x:Static Member="HorizontalAlignment.Center"/>

</Setter.Value>

</Setter>


-<Setter Property="Control.VerticalContentAlignment">


-<Setter.Value>

<x:Static Member="VerticalAlignment.Center"/>

</Setter.Value>

</Setter>


-<Setter Property="Border.BorderBrush">


-<Setter.Value>

<SolidColorBrush>#00FFFFFF</SolidColorBrush>

</Setter.Value>

</Setter>


-<Setter Property="Border.BorderThickness">


-<Setter.Value>

<Thickness>1,1,1,1</Thickness>

</Setter.Value>

</Setter>


-<Setter Property="TextElement.FontWeight">


-<Setter.Value>

<FontWeight>Black</FontWeight>

</Setter.Value>

</Setter>

</Style>

</Label.Style>



bien evidement s'il te faut l'integralité je te renverrais tout.
Tout mes controles se remplissent en fonctions de differentes requetes Sql dans le code Behind.........
Merci d'avance pour tes reponses ou tes elements de piste, j'avoue que je suis perdu.
Bonne fin de journée a toi.
0
Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 624
21 oct. 2019 à 19:23
Mmm, là je ne voies pas.

Par contre je m'interroge sur la pertinence de tous ces labels.

Pourquoi ne pas utiliser des contrôles prévus pour les data en collection (ListBox, wrappanel etc...)

0
dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021
22 oct. 2019 à 14:41
Salut Whismeril ,

Effectivement j'ai un paquet de label qu'ils serait très certainement judicieux de simplifier.
Que me conseil tu comme control qui accepterais plusieurs résultats sur une seule ligne.
Je m'explique:


Je rempli une floppé de label en fonction d'une requête Sql.
Cette requête me donne un nombre x de résultat en fonction de la valeur sélectionné dans la combobox et d'un paramètre.
Ces résultat s'affichent dans les labels (ici les 8 cases avec un zero dedans).
Tout fonctionnent parfaitement.
Quel control pourrait remplacer ces 8 labels pour ne faire plus q'une seul ligne ?

Merci D'avance.Bonne journée
A+
0
dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021
22 oct. 2019 à 18:13
Je me réponds a moi meme,
j'ai réussi a remplacer mes label par une Listbox contenant un Stackpanel en Orientation=horizontal.

 <ListBox x:Name="Spot1listBox" Style="{StaticResource BlockListbox}" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="8" >
                    <ListBoxItem  Style="{StaticResource ListboxItemBlockStyle}" >
                    </ListBoxItem>
                </ListBox


 <Style TargetType="ListBox" x:Key="BlockListbox">
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" ScrollViewer.CanContentScroll="False" Margin="0"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="ListBoxItem" x:Key="ListboxItemBlockStyle" >
        <Setter Property="Padding" Value="-15"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>


To be Continued.
0
Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 624
22 oct. 2019 à 18:53
Je comprends que les 8 cases correspondent à un enregistrement, là c'est assez logique de trouver 6 labels ou 6 textblocks (pour du texte c'est plus approprié que le label, on peut plus facilement utiliser un string format).

Mais pour plusieurs enregistrements du même type et bien on utilise une liste ou autre, dont la ligne est composée par exemple de labels

J'avais commencé à te faire un exemple, mais là je dois arrêter.
Et je me suis souvenu d'en avoir fait un qui pourrait être explicite dans cette discussion
https://www.commentcamarche.net/forum/affich-35872243-supprimer-des-boutons-crees-dynamiquement

0
dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021
3 nov. 2019 à 19:10
Salut Whismeril,
Suite a ton message j'ai remplacé mes labels par des listbox effectivement , c'est beaucoup plus simple et du coup j'ai moins de ligne de code....

Pour mon problème initial, de "duplicate name" je n'ai pas trouvé de solution.
j'ai donc changé mon fusil d’épaule et me suis rabattu sur Json.
Youpi j'ai réussi enfin a générer un fichier avec les données qui m’intéressent.

Dans mon usercontrol j'ai implanté Iserializable :
  [Serializable]
    public partial class BlockDirect : UserControl, ISerializable


et la méthode getdataobject:
 public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("direct", direct.Name);
            info.AddValue("Spot1", Spot1listBox.Items); info.AddValue("Spot2", Spot2listBox.Items); info.AddValue("Spot3", Spot3listBox.Items);
            info.AddValue("Spot4", Spot4listBox.Items); info.AddValue("Spot5", Spot5listBox.Items); info.AddValue("Spot6", Spot6listBox.Items);

            info.AddValue("patch1", SpotAdr1listBox.Items); info.AddValue("patch2", SpotAdr2listBox.Items); info.AddValue("patch3", SpotAdr3listBox.Items);
            info.AddValue("patch4", SpotAdr4listBox.Items); info.AddValue("patch5", SpotAdr5listBox.Items); info.AddValue("patch6", SpotAdr6listBox.Items);

            info.AddValue("notes1", Spot1DmxLbl.Content); info.AddValue("notes2", Spot2DmxLbl.Content); info.AddValue("notes3", Spot3DmxLbl.Content);
            info.AddValue("notes4", Spot4DmxLbl.Content); info.AddValue("notes5", Spot5DmxLbl.Content); info.AddValue("notes6", Spot6DmxLbl.Content);

            info.AddValue("CircuitName", Circuit1Cbx.Items); info.AddValue("CircuitNameSelected", Circuit1Cbx.SelectedItem);
            info.AddValue("totalph1", Ph1TotalWattLbl.Content); info.AddValue("totalph2", Ph2TotalWattLbl.Content); info.AddValue("totalph3", Ph3TotalWattLbl.Content);
            info.AddValue("totalamp1", Ph1TotalAmpLbl.Content); info.AddValue("totalamp2", Ph2TotalAmpLbl.Content); info.AddValue("totalamp3", Ph3TotalAmpLbl.Content);

            info.AddValue("ZoneName", ZoneCbx.Items);

        }


Pour serialiser les donnée dans un fichier:
  private void SaveBlockBtn_Click(object sender, RoutedEventArgs e)
        {

            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "UIElement File";
            dlg.DefaultExt = ".json";
            dlg.Filter = "Json File (.json)|*.json";
            Nullable<bool> result = dlg.ShowDialog();
            BlockDirect test = new BlockDirect();
           
            if (result == true)
            {
                string filename = dlg.FileName;
                string outputJSON = JsonConvert.SerializeObject(PatchStackPnl.Children);
                File.WriteAllText(filename, outputJSON);
            }
        }


Qui me donne un fichier Json:


Maintenant arrive le casse tête pour deserialiser tout ça....
J'ai essayer plusieurs truc avec des foreach mais je n'y arrive pas .
Aurais tu une piste?

Merci pour tout ce que tu as fait déjà fait pour moi.....
bonne soirée a toi et encore merci.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 624
3 nov. 2019 à 19:34
Désolé, json, je ne maitrise pas.

Mais bon, je suppose que la classe JsonConvert doit avoir de quoi déserialiser.

Cependant, que ce soit en json, ou en xml, est ce vraiment le contrôle qu'il faut serialiser ou les données qu'il représente.

Je m'explique, si tu as une liste de contacts, dans la base de donnée, il y a une table avec des noms, de prénoms, etc... pas des ligne de Listbox ou de ListView.
0
dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021
3 nov. 2019 à 19:54
En fait je sérialise bien les données qui sont dans le usercontrol , et qui proviennent d'une base de données je ne sérialise pas le usercontrol en tant que tel.
Il faudrais que je trouve une solution pour qu'a chaque fois que dans mon fichier Json il y a la valeur combobox.selectedItems il me fasse un stackpanel.children.add(usercontrol) avec la valeur combobox.selectitem dans cette combobox.(Aprés tout ce fera tout seul depuis le usercontrol).
Je vais réfléchir a tout ça encore un peu et revenir vers toi si je trouve une solution.
Merci encore pour ton aide.
Bonne soirée a toi.
0
Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 624
3 nov. 2019 à 20:35
C’est pas la peine de mettre un plus 1 à chacun de mes messages, c’est fait pour signaler dans un fil quel message t’a apporté la solution de ton problème.

Si tes données proviennent d’une base de données, quel est l’interêt de les réaliser?
0
dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021
3 nov. 2019 à 21:55
Désolé pour les plus je pensais que c’était de la bien séances.

Pour les données:
La base de donnée est "changeante" c'est a dire qu'elle est charger a partir d'un fichier Excel (Xlsx) elle change en fonction des projets.
L'utilisateur charge le Xlsx et met a jour la base de données.
l'user control est un condenser lisible de la base de donnée par zone.
Je pourrais détailler tout ça mais je pense que le Mp est preferable.
L'utilisateur doit pouvoir enregistrer son projet pour pouvoir y travailler ultérieurement.

Pour l'instant ce qui fonctionne:
-ouverture du fichier Xls qui met a jour la base de données.
-envoyer les données dans un datagrid.
-sauvegarde dans un fichier de la base de donnée donc du datagrid.
-User control qui synthétise les besoins par zone(avec plein de requête sql).

Je précise que j'ai réaliser un excel+vb qui fonctionne parfaitement pour faire tout ça mais mon but c'est de créer une appli.

A+ Merci encore .
0
Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 624
4 nov. 2019 à 00:02
Donc si je comprends bien, ta base de données est un condensé ou une association de plusieurs fichiers excel et tu veux stocker ce condensé dans un fichier json.

Quand tu dis
En fait je sérialise bien les données qui sont dans le usercontrol
, ce n'était pas le cas quand tu faisais du xml, le morceau de fichier que tu as posté au message 2 montre clairement que c'était le contrôle qui était sérialisé.

Et quand je voies
string outputJSON = JsonConvert.SerializeObject(PatchStackPnl.Children);
j'ai bien l'impression que c'est pareil, puisque le "Children" d'un StackPanel est un normalement un contrôle.
Et ton image n'aide pas franchement, puisqu'un json c'est un fichier texte et que cette image montre une représentation de ce fichier texte.

Comprends donc que je me sois interrogé.

Sur cette page, https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm on voit qu'il y a un certain nombre de méthode permettant de déserialiser des fichiers (et l'inverse aurait été surprenant).
As tu essayé?

J'imagine, partant du fait que tu as utiliser la surcharge avec un seul paramètre de SerializeObject, qu'il te faut, en retour, te servir de la surcharge avec un seul paramètre de DeserializeObject

0
dragonfly22000 Messages postés 49 Date d'inscription mardi 11 décembre 2018 Statut Membre Dernière intervention 13 avril 2021
5 nov. 2019 à 22:59
Salut Whismeril,
Effectivement dans mes premières tentatives en Xml je serialisait l’intégralité du usercontrol comme un crétin....
j'ai enfin trouvé la solution avec json.il me manque plus q'unpetit truc a trouver et ça en sera finit avec ce topic.

Ma solution:
Dans mon user control j'ai rajouté une classe
  public class BlockRootObject
        {
            public string CircuitNameSelected { get; set; }
            public string ZoneNameSelected { get; set; }
            public bool l1Check { get; set; }
            public bool l2Check { get; set; }
            public bool ZoneCheck { get; set; }
            public Point Position { get; set; }
        }

.
Puis rajouté ceci dans pour serialiser dans le usercontrol
 
 public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("CircuitNameSelected", Circuit1Cbx.SelectedItem);
            info.AddValue("ZoneNameSelected", ZoneCbx.SelectedItem);
            info.AddValue("l1Check", L1.IsChecked);
            info.AddValue("l2Check", L2.IsChecked);
            info.AddValue("ZoneCheck", TotalZoneChk.IsChecked);
            info.AddValue("Position", Direct.TransformToAncestor(mainWindow.PatchStackPnl).Transform(new Point(0, 0)));
        }


Puis toujours dans le usercontrol mais pour deserialiser:
 protected BlockDirect(SerializationInfo info, StreamingContext context)
        {
           BlockDirect blockdirect = new BlockDirect();
           blockdirect.Circuit1Cbx.SelectedItem = (string)info.GetValue("CircuitNameSelected", typeof(string));
           blockdirect.ZoneCbx.SelectedItem = (string)info.GetValue("ZoneNameSelected", typeof(string));
           blockdirect.L1.IsChecked = (bool)info.GetValue("l1Check", typeof(bool));
           blockdirect.L2.IsChecked = (bool)info.GetValue("l2Check", typeof(bool));
           blockdirect.TotalZoneChk.IsChecked = (bool)info.GetValue("ZoneCheck", typeof(bool));
           var currentpoint = blockdirect.TransformToAncestor(mainWindow.PatchStackPnl).Transform(new Point(0, 0));
           currentpoint = (Point)info.GetValue("Position", typeof(Point));
        }


Dans mon mainwidow pour deserialiser

 List<BlockDirect.BlockRootObject> BlockList = JsonConvert.DeserializeObject<List<BlockDirect.BlockRootObject>>(text);
                    for (int i = 0; i < BlockList.Count; i++)
                    {
                        BlockDirect blockDirect = new BlockDirect();
                        blockDirect.WireBox.Foreground = Brushes.Black;
                        blockDirect.BlockBox.Foreground = Brushes.Black;
                        blockDirect.TotalZoneBox.Foreground = Brushes.Black;
                        blockDirect.TotalMulti.Foreground = Brushes.Black;
                        blockDirect.Circuit1Cbx.SelectedItem = (BlockList[i].CircuitNameSelected);
                        blockDirect.ZoneCbx.SelectedItem = (BlockList[i].ZoneNameSelected);
                        blockDirect.L1.IsChecked = (BlockList[i].l1Check);
                        blockDirect.TotalZoneChk.IsChecked = (BlockList[i].ZoneCheck);

                        PatchStackPnl.Children.Add(blockDirect);
                    }


Mon fichierJson ressemble a ça:



Tout fonctionne parfaitement SAUF que je voudrais egalement deserialiser la vrai position du control qui est dans le stackpanel car je vais avoir bien évidemment des control différent et ce qui m’intéresse c'est également de pouvoir garder la mise en page.
Dans mon fichier Json j'ai bien la donnée serialisé.(position": "0,374.8")
mais je n'arrive pas a écrire correctement la ligne de code dans ma mainwidows pour deserialiser.

Merci encore pour tes réponses a bientôt.
Bonne soirée
0
Whismeril Messages postés 18416 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 juin 2023 624
6 nov. 2019 à 12:54
Pour ce que j’en sais un stackpanel panel empile (de haut en bas ou de droite à gauche selon l’orientation), en ne tenant compte que des marges.
Donc peut-être faut-il sauvegarder les marges.
0