質問編集履歴
1
追記:バインド実装
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,4 +9,78 @@
|
|
9
9
|
DependencySource は、DependencyObject と同じ Thread 上で作成する必要があります。
|
10
10
|
|
11
11
|
Backgroundに対してPropertyChangedイベントは利用できませんか?
|
12
|
-
どのような方法でもいいので、bindingする方法をご教示いただければと思います。
|
12
|
+
どのような方法でもいいので、bindingする方法をご教示いただければと思います。
|
13
|
+
|
14
|
+
|
15
|
+
<追記 : バインド実装>
|
16
|
+
-
|
17
|
+
xaml
|
18
|
+
```ここに言語を入力
|
19
|
+
<Label x:Name="lblB" Content="{Binding lbl_Context, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Background="{Binding lbl_Background, UpdateSourceTrigger=PropertyChanged}" />
|
20
|
+
```
|
21
|
+
|
22
|
+
-
|
23
|
+
MainWindow.vb
|
24
|
+
```ここに言語を入力
|
25
|
+
Sub New()
|
26
|
+
|
27
|
+
InitializeComponent()
|
28
|
+
|
29
|
+
'データバインディング
|
30
|
+
model = New DataModel
|
31
|
+
Me.DataContext = modl
|
32
|
+
|
33
|
+
'データモデルデータ更新スレッドスタート
|
34
|
+
End Sub
|
35
|
+
```
|
36
|
+
|
37
|
+
-
|
38
|
+
DataModel.vb
|
39
|
+
```ここに言語を入力
|
40
|
+
Public Class DataModel
|
41
|
+
Implements INotifyPropertyChanged
|
42
|
+
|
43
|
+
'テキスト
|
44
|
+
Private lblcont As String
|
45
|
+
'背景色
|
46
|
+
Private lblBackground As Brush = New SolidColorBrush(Colors.LightGray)
|
47
|
+
|
48
|
+
'表示更新通知イベント
|
49
|
+
Private Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
|
50
|
+
|
51
|
+
Private Sub OnPropertyChanged(propertyName As String)
|
52
|
+
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
|
53
|
+
End Sub
|
54
|
+
|
55
|
+
Private Sub SetProperty(Of T)(ByRef storage As T, value As T, propertyName As String)
|
56
|
+
Try
|
57
|
+
If Object.Equals(storage, value) Then
|
58
|
+
Return
|
59
|
+
End If
|
60
|
+
storage = value
|
61
|
+
OnPropertyChanged(propertyName)
|
62
|
+
Catch ex As Exception
|
63
|
+
'エラーログ出力処理
|
64
|
+
End Try
|
65
|
+
End Sub
|
66
|
+
|
67
|
+
'Content用
|
68
|
+
Public Property lbl_Context As String
|
69
|
+
Get
|
70
|
+
Return lblcont
|
71
|
+
End Get
|
72
|
+
Set(ByVal value As String)
|
73
|
+
SetProperty(lblcont, value, "lbl_Context")
|
74
|
+
End Set
|
75
|
+
End Property
|
76
|
+
|
77
|
+
Public Property lbl_Background As Brush
|
78
|
+
Get
|
79
|
+
Return lblBackground
|
80
|
+
End Get
|
81
|
+
Set(ByVal value As Brush)
|
82
|
+
SetProperty(lblBackground, value, "lbl_Background")
|
83
|
+
End Set
|
84
|
+
End Property
|
85
|
+
|
86
|
+
```
|