Statistiques
| Branche: | Révision:

root / GES_PAC / View / Controls / ChamberButtonView.xaml.cs @ 957bebf1

Historique | Voir | Annoter | Télécharger (1,851 ko)

1 e837cdf1 lbihannic
using GES_PAC.ViewModel;
2
using GES_PAC.ViewModel.Controls;
3 12ddf7ef lbihannic
4 e837cdf1 lbihannic
namespace GES_PAC.View.Controls;
5
6
public partial class ChamberButtonView : ContentView
7 12ddf7ef lbihannic
{
8 e837cdf1 lbihannic
    public static readonly BindableProperty ChamberIdProperty =
9
        BindableProperty.Create(nameof(ChamberId), typeof(int), typeof(ChamberButtonView), default(int), propertyChanged: OnChamberIdChanged);
10
11 957bebf1 lbihannic
    public static readonly BindableProperty ParentViewModelProperty =
12
        BindableProperty.Create(nameof(ParentViewModel), typeof(ChambersViewModel), typeof(ChamberButtonView), propertyChanged: OnParentChanged);
13 e837cdf1 lbihannic
14 957bebf1 lbihannic
    public ChambersViewModel? ParentViewModel
15 e837cdf1 lbihannic
    {
16 957bebf1 lbihannic
        get => (ChambersViewModel?)GetValue(ParentViewModelProperty);
17
        set => SetValue(ParentViewModelProperty, value);
18 e837cdf1 lbihannic
    }
19
20
    public int ChamberId
21
    {
22
        get => (int)GetValue(ChamberIdProperty);
23
        set => SetValue(ChamberIdProperty, value);
24
    }
25
    public ChamberButtonView()
26
    {
27
        InitializeComponent();
28
    }
29
30
    protected override void OnBindingContextChanged()
31 12ddf7ef lbihannic
    {
32 e837cdf1 lbihannic
        base.OnBindingContextChanged();
33 12ddf7ef lbihannic
34 957bebf1 lbihannic
        if (BindingContext is not ChamberButtonViewModel && ChamberId != 0 && ParentViewModel != null)
35 12ddf7ef lbihannic
        {
36 957bebf1 lbihannic
            var vm = new ChamberButtonViewModel(ChamberId, ParentViewModel);
37 e837cdf1 lbihannic
            BindingContext = vm;
38 42456640 lbihannic
            vm.UpdateProperties();
39 12ddf7ef lbihannic
        }
40 e837cdf1 lbihannic
    }
41
    private static void OnChamberIdChanged(BindableObject bindable, object oldValue, object newValue)
42
    {
43
        var control = (ChamberButtonView)bindable;
44
        int id = (int)newValue;
45 12ddf7ef lbihannic
46 e837cdf1 lbihannic
        if (control.BindingContext is ChamberButtonViewModel vm)
47 12ddf7ef lbihannic
        {
48 e837cdf1 lbihannic
            vm.UpdateChamberId(id);
49 12ddf7ef lbihannic
        }
50 e837cdf1 lbihannic
    }
51 957bebf1 lbihannic
    private static void OnParentChanged(BindableObject bindable, object oldValue, object newValue)
52 e837cdf1 lbihannic
    {
53
        if (bindable is ChamberButtonView view)
54 12ddf7ef lbihannic
        {
55 e837cdf1 lbihannic
            view.OnBindingContextChanged();
56 12ddf7ef lbihannic
        }
57
    }
58 e837cdf1 lbihannic
59 12ddf7ef lbihannic
}