Bonjour,
j'ai développé un usercontrol ucTimeRange qui expose une DependencyProperty de type TimeRange.
J'aimerais binder à cette DependencyProperty une propriété de type string. Ainsi j'aimerais utiliser un IValueConverter pour convertir la propriété de type string en TimeRange et inversement.
La méthode Convert est appelé lorsque je modifie la valeur de la propriété de type string mais si je sélectionne une autre valeur dans le combo ou saisi une valeur differente dans le textbox, la méthode ConvertBack n'est pas appelé.
Je ne comprends pas pourquoi.
Voici la définition du TimeRange:
Code c# : 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 public class TimeRange { public TimeFormatEnum Format { get; set; } public int Value { get; set; } } public enum TimeFormatEnum { [Description("seconde")] s, //seconde [Description("minute")] m, // minute [Description("heure")] h, // heure [Description("jour")] d, // jour [Description("semaine")] w, // semaine [Description("mois")] mo, // mois [Description("année")] y // année }
Voici le code du UserControl:
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 <TextBox Name="txtMult" Grid.Column="0" Padding="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" VerticalContentAlignment="Center" TextAlignment="Right" Text="{Binding ElementName=ucTR, Path=TRange.Value}"/> <ComboBox Name="cbTime" Grid.Column="2" Padding="1,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{local:EnumDescriptionsExtension lib:TimeFormatEnum}" DisplayMemberPath="Item2" SelectedValuePath="Item1" SelectedValue="{Binding ElementName=ucTR, Path=TRange.Format,Mode=TwoWay}" />
Voici le code du TimeRangeConverter:
Code c# : 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 public class TimeRangeConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return DependencyProperty.UnsetValue; return ParseTimeRange(value.ToString()); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return DependencyProperty.UnsetValue; TimeRange useValue = (TimeRange)value; return useValue.Value.ToString() + useValue.Format.ToString(); } private TimeRange ParseTimeRange(string s) { TimeRange tr = new TimeRange(); if (!string.IsNullOrEmpty(s)) { // Obtient le code du TimeFormat string codeFormat = new string(s.Where(c => Char.IsLetter(c)).ToArray()); // Obtient la valeur du TimeRange int value = 0; if (int.TryParse(s.Replace(codeFormat, ""), out value)) { tr.Format = (TimeFormatEnum)Enum.Parse(typeof(TimeFormatEnum), codeFormat); tr.Value = value; } } return tr; } }
J'ai bindé ce ValueConverter comme suit:
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part <my:ucTimeRange Grid.Row="0" Grid.Column="2" x:Name="ucTimeRge" HorizontalAlignment="Left" Height="24" Width="181" IsEnabled="{Binding ElementName=rdbRealTime, Path=IsChecked}" TRange="{Binding Path=VTViewModel.RealTimeRange, Converter={StaticResource trConverter}}"/>
Est-ce que vous auriez une idée ?
Merci d'avance,
Zoax.
Partager