質問編集履歴

1

追記:バインド実装

2018/12/04 04:51

投稿

necodow
necodow

スコア15

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,151 @@
21
21
  Backgroundに対してPropertyChangedイベントは利用できませんか?
22
22
 
23
23
  どのような方法でもいいので、bindingする方法をご教示いただければと思います。
24
+
25
+
26
+
27
+
28
+
29
+ <追記 : バインド実装>
30
+
31
+ -
32
+
33
+ xaml
34
+
35
+ ```ここに言語を入力
36
+
37
+ <Label x:Name="lblB" Content="{Binding lbl_Context, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Background="{Binding lbl_Background, UpdateSourceTrigger=PropertyChanged}" />
38
+
39
+ ```
40
+
41
+
42
+
43
+ -
44
+
45
+ MainWindow.vb
46
+
47
+ ```ここに言語を入力
48
+
49
+ Sub New()
50
+
51
+
52
+
53
+ InitializeComponent()
54
+
55
+
56
+
57
+ 'データバインディング
58
+
59
+ model = New DataModel
60
+
61
+ Me.DataContext = modl
62
+
63
+
64
+
65
+ 'データモデルデータ更新スレッドスタート
66
+
67
+ End Sub
68
+
69
+ ```
70
+
71
+
72
+
73
+ -
74
+
75
+ DataModel.vb
76
+
77
+ ```ここに言語を入力
78
+
79
+ Public Class DataModel
80
+
81
+ Implements INotifyPropertyChanged
82
+
83
+
84
+
85
+ 'テキスト
86
+
87
+ Private lblcont As String
88
+
89
+ '背景色
90
+
91
+ Private lblBackground As Brush = New SolidColorBrush(Colors.LightGray)
92
+
93
+
94
+
95
+ '表示更新通知イベント
96
+
97
+ Private Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
98
+
99
+
100
+
101
+ Private Sub OnPropertyChanged(propertyName As String)
102
+
103
+ RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
104
+
105
+ End Sub
106
+
107
+
108
+
109
+ Private Sub SetProperty(Of T)(ByRef storage As T, value As T, propertyName As String)
110
+
111
+ Try
112
+
113
+ If Object.Equals(storage, value) Then
114
+
115
+ Return
116
+
117
+ End If
118
+
119
+ storage = value
120
+
121
+ OnPropertyChanged(propertyName)
122
+
123
+ Catch ex As Exception
124
+
125
+ 'エラーログ出力処理
126
+
127
+ End Try
128
+
129
+ End Sub
130
+
131
+
132
+
133
+ 'Content用
134
+
135
+ Public Property lbl_Context As String
136
+
137
+ Get
138
+
139
+ Return lblcont
140
+
141
+ End Get
142
+
143
+ Set(ByVal value As String)
144
+
145
+ SetProperty(lblcont, value, "lbl_Context")
146
+
147
+ End Set
148
+
149
+ End Property
150
+
151
+
152
+
153
+ Public Property lbl_Background As Brush
154
+
155
+ Get
156
+
157
+ Return lblBackground
158
+
159
+ End Get
160
+
161
+ Set(ByVal value As Brush)
162
+
163
+       SetProperty(lblBackground, value, "lbl_Background")
164
+
165
+ End Set
166
+
167
+ End Property
168
+
169
+
170
+
171
+ ```