Исходный код вики Change notify
Редактировал(а) Alexandr Fokin 2024/08/25 18:10
Последние авторы
author | version | line-number | content |
---|---|---|---|
1 | |(% style="width:81px" %) |(% style="width:1356px" %)((( | ||
2 | C# - Intercepting property changes in subclasses | ||
3 | [[https:~~/~~/stackoverflow.com/questions/6078859/c-sharp-intercepting-property-changes-in-subclasses>>https://stackoverflow.com/questions/6078859/c-sharp-intercepting-property-changes-in-subclasses]] | ||
4 | |||
5 | ObservableObject | ||
6 | [[https:~~/~~/learn.microsoft.com/ru-ru/dotnet/communitytoolkit/mvvm/observableobject>>https://learn.microsoft.com/ru-ru/dotnet/communitytoolkit/mvvm/observableobject]] | ||
7 | ))) | ||
8 | |(% style="width:81px" %) |(% style="width:1356px" %)[[Привязка>>doc:Разработка.NET.Frameworks and Apps.GUI.WPF.Механизмы.Привязка.WebHome]] | ||
9 | |(% style="width:81px" %) |(% style="width:1356px" %)((( | ||
10 | |{{code language="c#"}}/// <summary> | ||
11 | /// Контейнер с INotifyPropertyChanged | ||
12 | /// </summary> | ||
13 | public class ObservableContainer<T> | ||
14 | : INotifyPropertyChanged, | ||
15 | IEquatable<ObservableContainer<T>>, | ||
16 | IEquatable<T> | ||
17 | { | ||
18 | #region prop | ||
19 | |||
20 | private T _data; | ||
21 | |||
22 | public event PropertyChangedEventHandler PropertyChanged; | ||
23 | |||
24 | public T Data | ||
25 | { | ||
26 | get => _data; | ||
27 | set | ||
28 | { | ||
29 | _data = value; | ||
30 | PropertyChanged?.Invoke( | ||
31 | this, | ||
32 | new PropertyChangedEventArgs(nameof(Data)) | ||
33 | ); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | #endregion | ||
38 | |||
39 | |||
40 | #region ctor | ||
41 | |||
42 | public ObservableContainer() { } | ||
43 | public ObservableContainer(T data) | ||
44 | { | ||
45 | _data = data; | ||
46 | } | ||
47 | |||
48 | #endregion | ||
49 | |||
50 | |||
51 | #region override | ||
52 | |||
53 | public override string ToString() | ||
54 | { | ||
55 | return _data?.ToString() ?? "null"; | ||
56 | } | ||
57 | |||
58 | public override int GetHashCode() | ||
59 | { | ||
60 | return _data?.GetHashCode() ?? 0; | ||
61 | } | ||
62 | |||
63 | public override bool Equals(object obj) | ||
64 | { | ||
65 | if (obj is ObservableContainer<T> o1) | ||
66 | { | ||
67 | return Equals(other: o1); | ||
68 | } | ||
69 | else if (obj is T o2) | ||
70 | { | ||
71 | return Equals(other: o2); | ||
72 | } | ||
73 | |||
74 | return false; | ||
75 | } | ||
76 | |||
77 | public bool Equals(ObservableContainer<T> other) | ||
78 | { | ||
79 | if (other.Data == null && Data == null) | ||
80 | { | ||
81 | return true; | ||
82 | } | ||
83 | if (other.Data != null) | ||
84 | { | ||
85 | other.Data.Equals(Data); | ||
86 | } | ||
87 | if (Data != null) | ||
88 | { | ||
89 | Data.Equals(other.Data); | ||
90 | } | ||
91 | |||
92 | return false; | ||
93 | } | ||
94 | |||
95 | public bool Equals(T other) | ||
96 | { | ||
97 | if (other == null && Data == null) | ||
98 | { | ||
99 | return true; | ||
100 | } | ||
101 | if (other != null) | ||
102 | { | ||
103 | other.Equals(Data); | ||
104 | } | ||
105 | if (Data != null) | ||
106 | { | ||
107 | Data.Equals(other); | ||
108 | } | ||
109 | |||
110 | return false; | ||
111 | } | ||
112 | |||
113 | #endregion | ||
114 | |||
115 | #region operator | ||
116 | |||
117 | public static implicit operator ObservableContainer<T>(T x) | ||
118 | { | ||
119 | return new ObservableContainer<T>(x); | ||
120 | } | ||
121 | public static implicit operator T(ObservableContainer<T> x) | ||
122 | { | ||
123 | return x.Data; | ||
124 | } | ||
125 | |||
126 | #endregion | ||
127 | }{{/code}} | ||
128 | |{{code language="c#"}}public class TestModel2 | ||
129 | { | ||
130 | public ObservableContainer<string> Property1 { get; set; } | ||
131 | = new ObservableContainer<string>(); | ||
132 | public ObservableContainer<string> Property2 { get; set; } | ||
133 | = new ObservableContainer<string>(); | ||
134 | }{{/code}} | ||
135 | |{{code language="xml"}}<UniformGrid Rows="2" Columns="2"> | ||
136 | <TextBox Text="Prop1.Data"></TextBox> | ||
137 | <TextBox Text="{Binding Path=Property1.Data, Mode=OneWay}"></TextBox> | ||
138 | <Button Content="Right Bottom" ></Button> | ||
139 | |||
140 | <TextBox Text="Prop2.Data"></TextBox> | ||
141 | <TextBox Text="{Binding Path=Property2.Data, Mode=TwoWay}"></TextBox> | ||
142 | <Button Content="Right Bottom" Click="Button_Click" ></Button> | ||
143 | </UniformGrid>{{/code}} | ||
144 | ))) | ||
145 | |(% style="width:81px" %) |(% style="width:1356px" %) | ||
146 | |||
147 |