Bonjour,
Je tente ma première appli en WPF, donc bcp de nouveautés pour moi, et donc d'embuches à surmonter.
J'apprends, mais là je coince. J'ai beau lire, visiblement qqch m'échappe parce que je n'arrive pas à faire ce dont j'ai besoin.
Je tente de passer un objet d'un UserControl vers un autre, mais je n'y arrive pas (j'ai trouvé pléthore d'explications sur les DependencyProperty, mais visiblement qqch m'échappe).
J'essaye de passer l'objet NewMinute (de type minutes) mais elle reste imperturbablement à null du côté du "sous"-UserControl.
Pourquoi l'objet NewMinute, qui n'est pas null dans le UserControl parent, reste null dans le UserControl enfant ?
Qu'ai-je mal fait ?
Aussi, vu que visiblement qqch m'échappe pour le passage dans un sens, est ce que que je dois faire qqch de particulier pour le retour ? Il me semble que via le DependencyProperty, le NewMinute du UserControl parent devrait être mis à jour "automatiquement", mais est-ce que j'ai bien compris ou est-ce que je dois ajouter qqch pour le retour de la nouvelle minute vers le UserControl parent à la fin du AddBTN_Click ?
Merci de votre aide, et surtout de vos explications.
Voici comment j'ai inséré le UserForm dans le xaml du UserControl parent
Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 <local:ALotSelectUC Grid.Column="1" VerticalAlignment="Top" Minute="{Binding Path=NewMinute}" />
Et voici mon "sous"-UserControl
C#
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
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
102
103
104 <UserControl x:Class="Greffe.UserControls.ALotSelectUC" 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" xmlns:local="clr-namespace:Greffe.UserControls" d:DataContext="{d:DesignInstance Type=local:ALotSelectUC}" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="250"> <UserControl.Resources> <ControlTemplate x:Key="ValidationErrorTemplate"> <DockPanel> <Border BorderBrush="red" BorderThickness="2"> <AdornedElementPlaceholder /> </Border> </DockPanel> </ControlTemplate> <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}"> <Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)/ErrorContent}"/> </Trigger> </Style.Triggers> </Style> </UserControl.Resources> <Grid> <StackPanel Name="AddALotSP" Orientation ="Vertical" > <Label Content="Cadastre : " /> <ComboBox Name="CadastreCB" ItemsSource="{Binding Path=Cadastres, Mode =OneWay}" DisplayMemberPath="Description" SelectedItem="{Binding Path=SelectedCadastre}" IsEditable="True" IsTextSearchEnabled="False" TextBoxBase.TextChanged="CadastreCB_TextChanged"> <ComboBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </ComboBox.ItemsPanel> </ComboBox> <Label Content="Désignation secondaire : " Margin="0,20,0,0"/> <ComboBox Name="DesSecCB" ItemsSource="{Binding Path=DesSecs, Mode = OneWay}" DisplayMemberPath="Description" SelectedItem="{Binding Path=SelectedDesSec}"> <ComboBox.Style> <Style TargetType="ComboBox"> <Setter Property="IsEnabled" Value="True"/> <Style.Triggers> <DataTrigger Binding="{Binding Path=SelectedCadastre}" Value="{x:Null}"> <Setter Property="IsEnabled" Value="False"/> </DataTrigger> <DataTrigger Binding="{Binding Path=DesSecs.Count}" Value="1"> <Setter Property="SelectedIndex" Value="0" /> </DataTrigger> </Style.Triggers> </Style> </ComboBox.Style> </ComboBox> <Label Content="Lot : " Margin="0,20,0,0"/> <ComboBox Name="NoAlotCB" ItemsSource="{Binding Path=AncLots, Mode = OneWay}" DisplayMemberPath="no_lot" SelectedItem="{Binding Path=SelectedALot}"> <ComboBox.Style> <Style TargetType="ComboBox"> <Setter Property="IsEnabled" Value="True"/> <Style.Triggers> <DataTrigger Binding="{Binding Path=SelectedDesSec}" Value="{x:Null}"> <Setter Property="IsEnabled" Value="False"/> </DataTrigger> <DataTrigger Binding="{Binding Path=AncLots.Count}" Value="1"> <Setter Property="SelectedIndex" Value="0" /> </DataTrigger> </Style.Triggers> </Style> </ComboBox.Style> </ComboBox> <!--<TextBox Name="NoALotTB" Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}" Style="{StaticResource textBoxInError}" VerticalAlignment="Center" Text="{Binding ALotFrom, ValidatesOnExceptions=True}" />--> <StackPanel x:Name="PtieSP" Orientation="Horizontal" Margin="0,20,0,0"> <RadioButton x:Name="PtieRB" Content="Ptie" IsChecked="True" /> <RadioButton x:Name="EntierRB" Content="Entier" Margin="30,0,0,0" /> </StackPanel> <Button Name="AddBTN" Content="Ajouter" Margin="0,20,0,0" Click="AddBTN_Click" IsEnabled="{Binding EnableAddBTN}"/> </StackPanel> </Grid> </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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 public partial class ALotSelectUC : UserControl, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string info) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info)); } private List<Cadastre> _cadastres; private List<anc_lots> _ancLots; private Cadastre _selectedCadastre; private DesignationSecondaire _selectedDesSec; private anc_lots _selectedALot; public ALotSelectUC() { InitializeComponent(); DataContext = this; Cadastres = CadastresStatiques.ListeCadastres; } public static readonly DependencyProperty MinuteProperty = DependencyProperty.Register( "Minute", typeof(minutes), typeof(ALotSelectUC)); public minutes Minute { get { return (minutes)GetValue(MinuteProperty); } set { SetValue(MinuteProperty, value); } } public List<Cadastre> Cadastres { get { return _cadastres; } set { _cadastres = value; OnPropertyChanged("Cadastres"); } } public List<DesignationSecondaire> DesSecs { get { if (SelectedCadastre != null) return SelectedCadastre.DesignationsSecondaires.Where(ds => ds.DesignationInexistante == false).ToList(); return null; } } public List<anc_lots> AncLots { get { return _ancLots; } set { _ancLots = value; OnPropertyChanged("AncLots"); OnPropertyChanged("EnableAddBTN"); } } public Cadastre SelectedCadastre { get { return _selectedCadastre; } set { _selectedCadastre = value; if (value == null) SelectedDesSec = null; OnPropertyChanged("SelectedCadastre"); OnPropertyChanged("DesSecs"); } } public DesignationSecondaire SelectedDesSec { get { return _selectedDesSec; } set { _selectedDesSec = value; SelectedALot = null; LoadALots(); OnPropertyChanged("SelectedDesSec"); } } public anc_lots SelectedALot { get { return _selectedALot; } set { _selectedALot = value; OnPropertyChanged("SelectedALot"); OnPropertyChanged("EnableAddBTN"); } } public bool EnableAddBTN { get { return SelectedCadastre != null && SelectedDesSec != null && SelectedALot != null; } } private void CadastreCB_TextChanged(object sender, TextChangedEventArgs e) { ComboBox cb = sender as ComboBox; cb.IsDropDownOpen = true; if (!string.IsNullOrWhiteSpace(cb.Text)) { string target = cb.Text; try { var compareInfo = CultureInfo.CurrentCulture.CompareInfo; var result = Cadastres.Where(c => compareInfo.IndexOf(c.Description, target, CompareOptions.OrdinalIgnoreCase) > -1).ToList(); if (result.Count == 1) { SelectedCadastre = result[0]; cb.ItemsSource = Cadastres; } else cb.ItemsSource = result; } catch { SelectedCadastre = null; cb.ItemsSource = Cadastres; } } else { SelectedCadastre = null; cb.ItemsSource = Cadastres; } } private void AddBTN_Click(object sender, RoutedEventArgs e) { if (Minute != null && !Minute.minutes_anc_lots.Any(mal => mal.id_anc_lot == SelectedALot.id_anc_lot)) { minutes_anc_lots NewMAL = new minutes_anc_lots() { id_minute = Minute.id_minute, id_anc_lot = SelectedALot.id_anc_lot, anc_lots = SelectedALot, ptie = PtieRB.IsChecked, minutes = Minute }; Minute.minutes_anc_lots.Add(NewMAL); } } private void LoadALots() { if (SelectedDesSec != null) { try { using (var ctr = new GreffeAdoModelContainer()) { var result = ctr.anc_lots .Where(al => al.code_cad == SelectedCadastre.Code && al.code_ds == SelectedDesSec.Code) .OrderBy(al => al.no_lot) .ToList(); AncLots = result; } } catch { AncLots = null; } } else AncLots = null; } }
Partager