Bonjour à tous.
J'ai ce que je pourrais qualifier un problème de Binding à rebonds.
J'ai ma classe de données (ParameterForCapture.cs) :
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 public class ParameterForCapture : ParameterForDisplay, INotifyPropertyChanged { private bool boolValue; public bool BoolValue { get { return boolValue; } set { boolValue = value; Notify("BoolValue"); } } protected void Notify(string propName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } }
Il y'a le Notify qui permet de binder. Je binde cette donnée dans une fenêtre (WidgetBoolean) de deux manières au format texte et dans un controle créé par mes soins, une LED appelée BooleanLED :
BooleanLED.xaml
Le problème apparaît ici, le binding texte se met bien à jour mais pas le binding sur mon contrôle.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 <controls:BooleanLED Margin="5" Grid.Row="0" Grid.Column="1" IsOn="{Binding Path=BoolValue}" MinWidth="30" MinHeight="30"/> <Label Content="{Binding Path=BoolValue}" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"/>
Voici le code de mon contrôle (BooleanLED.xaml) :
BooleanLED.xaml.cs
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 <UserControl x:Class="Antarys_v2.GUI.Controls.BooleanLED" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinWidth="10" MinHeight="10"> <UserControl.Resources> <LinearGradientBrush x:Key="GreenGradient" StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Green" Offset="0" /> <GradientStop Color="LawnGreen" Offset="1" /> </LinearGradientBrush> <LinearGradientBrush x:Key="RedGradient" StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Orange" Offset="0" /> <GradientStop Color="Red" Offset="1" /> </LinearGradientBrush> <Style x:Name="style_Ellipse" x:Key="EllipseStyle" TargetType="{x:Type Ellipse}" > <Style.Triggers> <DataTrigger Binding="{Binding Path=IsOn}" Value="false"> <Setter Property="Fill" Value="{StaticResource RedGradient}" /> </DataTrigger> <DataTrigger Binding="{Binding Path=IsOn}" Value="true"> <Setter Property="Fill" Value="{StaticResource GreenGradient}" /> </DataTrigger> </Style.Triggers> </Style> </UserControl.Resources> <Grid x:Name="mainGrid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="20*" /> <RowDefinition Height="6*" /> </Grid.RowDefinitions> <Ellipse Grid.RowSpan="3" Grid.ColumnSpan="3"> <Ellipse.Fill> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Gray" Offset="0" /> <GradientStop Color="LightGray" Offset="1" /> </LinearGradientBrush> </Ellipse.Fill> </Ellipse> <Ellipse Grid.RowSpan="3" Grid.ColumnSpan="3" Margin="1" Style="{StaticResource EllipseStyle}" /> <Ellipse Grid.Column="1" Grid.Row="1" Margin="1"> <Ellipse.Fill> <RadialGradientBrush Center="0.5,0.1" GradientOrigin="0.5,0.1" RadiusX="0.7" RadiusY="0.5"> <GradientStop Color="#efff" Offset="0" /> <GradientStop Color="Transparent" Offset="1" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <Label Content="{Binding IsOn}" /> </Grid> </UserControl>
Vous l'avez compris, y'a un moment ou la LED foire et ne se met pas à jour, pourquoi ?
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 namespace Antarys_v2.GUI.Controls { /// <summary> /// Logique d'interaction pour BooleanLED.xaml /// </summary> public partial class BooleanLED : UserControl { public static readonly DependencyProperty IsOnProperty; public bool IsOn { get { return (Boolean)GetValue(IsOnProperty); } set { SetValue(IsOnProperty, value); } } public BooleanLED() { InitializeComponent(); mainGrid.DataContext = this; } static BooleanLED() { PropertyChangedCallback isOnChanged = new PropertyChangedCallback(OnIsOnChanged); PropertyMetadata isOnMetadata = new PropertyMetadata((bool)false, isOnChanged); IsOnProperty = DependencyProperty.Register("IsOn", typeof(bool), typeof(BooleanLED), isOnMetadata); } static void OnIsOnChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { BooleanLED myLed = (BooleanLED)target; myLed.IsOn = (bool)e.NewValue; } }
Partager