回答編集履歴
1
最初から出してくれてりゃなぁw
test
CHANGED
@@ -1,216 +1,54 @@
|
|
1
|
-
|
1
|
+
`Script`で`UserControl1`を作っているなら、そこで直接変えられます(エスパー回答通り)
|
2
|
+
もしかしたら`UserControl1`内で、`A`を使えるのかもしれません(`A`次第)
|
2
3
|
|
3
|
-
```xml:MainWindow.xaml
|
4
|
-
<Window
|
5
|
-
|
4
|
+
グリッド直置きはよくないですが、変更点が多いと混乱させそうなので目をつぶります^^;
|
6
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
7
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
8
|
-
xmlns:local="clr-namespace:Q21wjdsxlvpvelo"
|
9
|
-
Width="800"
|
10
|
-
Height="450">
|
11
|
-
<StackPanel>
|
12
|
-
<local:UserControl1 x:Name="userControl1" />
|
13
|
-
<Button Click="Button_Click" Content="change" />
|
14
|
-
</StackPanel>
|
15
|
-
</Window>
|
16
|
-
```
|
17
|
-
```cs:
|
5
|
+
```cs:Script.cs
|
6
|
+
using System;
|
7
|
+
using System.Linq;
|
8
|
+
using System.Text;
|
18
9
|
using System.Windows;
|
10
|
+
using System.Windows.Controls;
|
11
|
+
using System.Collections.Generic;
|
12
|
+
using System.Reflection;
|
13
|
+
using System.Runtime.CompilerServices;
|
19
14
|
using System.Windows.Media;
|
20
|
-
using
|
15
|
+
using ●●●●●●●●●●●●●●●●;
|
16
|
+
using ●●●●●●●●●●●●●●●●;
|
21
17
|
|
22
|
-
|
23
|
-
namespace
|
18
|
+
namespace ●●●●●●●
|
24
19
|
{
|
25
|
-
public class
|
20
|
+
public class Script
|
26
21
|
{
|
27
|
-
// 交互に変えてるだけ(特に意味はない)
|
28
|
-
public static string DeviceId => i++ % 2 == 0 ? "debaisu" : "debaityu";
|
29
|
-
private static int i;
|
30
|
-
}
|
31
|
-
|
32
|
-
|
33
|
-
// INotifyPropertyChanged実装はお好みで
|
34
|
-
// [方法: プロパティの変更通知を実装する - WPF .NET Framework | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/desktop/wpf/data/how-to-implement-property-change-notification)
|
35
|
-
public class ViewModel : ObservableObject
|
36
|
-
{
|
37
|
-
public string DeviceId { get => _DeviceId; set => SetProperty(ref _DeviceId, value); }
|
38
|
-
private string _DeviceId = "ラベルに表示する文字";
|
39
|
-
}
|
40
|
-
|
41
|
-
|
42
|
-
public partial class MainWindow : Window
|
43
|
-
{
|
44
|
-
private static BrushConverter brushConverter = new BrushConverter();
|
45
|
-
|
46
|
-
private readonly ViewModel vm = new ViewModel();
|
47
|
-
|
48
|
-
public
|
22
|
+
public Script()
|
49
23
|
{
|
50
|
-
InitializeComponent();
|
51
|
-
|
52
|
-
// DataContextにViewModelのインスタンスをセット
|
53
|
-
// ViewModelのプロパティ(DeviceId)が、xamlで"{Binding DeviceId}"のように参照・設定可能に
|
54
|
-
// [データ バインディングの概要 - WPF .NET | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/desktop/wpf/data/)
|
55
|
-
DataContext = vm;
|
56
24
|
}
|
57
25
|
|
26
|
+
[MethodImpl(MethodImplOptions.NoInlining)]
|
58
|
-
p
|
27
|
+
public void Execute(●〇〇〇〇〇〇●〇〇〇 〇〇〇〇〇〇, System.Windows.Window window)
|
59
28
|
{
|
29
|
+
var control = new UserControl1();
|
60
|
-
|
30
|
+
window.Content = control;
|
31
|
+
window.Height = 842;
|
32
|
+
window.Width = 595;
|
61
33
|
|
34
|
+
string DeviceID = A.DeviceId;
|
35
|
+
control.DeviceID_tb.Text = DeviceID;
|
62
36
|
|
63
|
-
// メインで変える(label1)
|
64
|
-
userControl1.label1.Content = deviceId;
|
65
|
-
switch (
|
37
|
+
switch (DeviceID)
|
66
38
|
{
|
67
39
|
case "debaisu":
|
40
|
+
// エスパー回答するならUserControlにx:Nameを付けて、userControl.ラベル名_lb.Background...とメイン側で変える。
|
68
|
-
|
41
|
+
control.DeviceOk_lb.Background = (Brush)new BrushConverter().ConvertFrom("#FFF39915");
|
42
|
+
control.DeviceOk_lb.Content = "OK";
|
43
|
+
|
44
|
+
// OK・NG 2択で色も共通ならTriggerで色を切り替えるStyleを作っておくと、Contentを変えるだけで色も変わって楽
|
45
|
+
control.DeviceOk_lb2.Content = "OK";
|
69
46
|
break;
|
70
47
|
default:
|
71
|
-
|
48
|
+
control.DeviceOk_lb.Background = (Brush)new BrushConverter().ConvertFrom("#FF24E827");
|
72
|
-
|
49
|
+
control.DeviceOk_lb.Content = "NG";
|
73
|
-
}
|
74
50
|
|
75
|
-
|
76
|
-
// UserControlで変える(label2)
|
77
|
-
userControl1.ChangeText(deviceId);
|
78
|
-
|
79
|
-
|
80
|
-
// ViewModelで変える(label3・label4・label5)
|
81
|
-
// INotifyPropertyChangedを実装しているクラスのプロパティ変更は、Viewに通知が飛び自動で変更が反映される
|
82
|
-
vm.DeviceId = deviceId;
|
83
|
-
|
84
|
-
// label3はその際にTriggerが働き、(DeviceIdをバインドしている)Contentが条件を満たしていればBackground変更
|
85
|
-
// label4はその際にDataTriggerが働き、DeviceIdが条件を満たしていればBackground変更
|
86
|
-
// label5はBackgroundにDeviceIdをバインドして、条件にあうブラシをDeviceId2BrushConverterが都度返す
|
87
|
-
}
|
88
|
-
}
|
89
|
-
}
|
90
|
-
```
|
91
|
-
|
92
|
-
|
93
|
-
```xml:UserControl1.xaml
|
94
|
-
<UserControl
|
95
|
-
x:Class="Q21wjdsxlvpvelo.UserControl1"
|
96
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
97
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
98
|
-
xmlns:local="clr-namespace:Q21wjdsxlvpvelo">
|
99
|
-
<UserControl.Resources>
|
100
|
-
|
101
|
-
<!-- Converterインスタンス生成 -->
|
102
|
-
<local:DeviceId2BrushConverter
|
103
|
-
|
51
|
+
control.DeviceOk_lb2.Content = "NG";
|
104
|
-
Ng="#FF24E827"
|
105
|
-
Ok="#FFF39915" />
|
106
|
-
</UserControl.Resources>
|
107
|
-
<StackPanel>
|
108
|
-
<!--#region コードビハインド-->
|
109
|
-
|
110
|
-
<!-- MainWindowから直で変更 -->
|
111
|
-
<Label x:Name="label1" Content="ラベルに表示する文字" />
|
112
|
-
|
113
|
-
<!-- UserControlのメソッドから変更 -->
|
114
|
-
<Label x:Name="label2" Content="ラベルに表示する文字" />
|
115
|
-
|
116
|
-
<!--#endregion-->
|
117
|
-
|
118
|
-
|
119
|
-
<!--#region MVVM-->
|
120
|
-
|
121
|
-
<!-- Trigger -->
|
122
|
-
<Label x:Name="label3" Content="{Binding DeviceId}">
|
123
|
-
<Label.Style>
|
124
|
-
<Style TargetType="{x:Type Label}">
|
125
|
-
<Setter Property="Background" Value="#FF24E827" />
|
126
|
-
<Style.Triggers>
|
127
|
-
<Trigger Property="Content" Value="debaisu">
|
128
|
-
<Setter Property="Background" Value="#FFF39915" />
|
129
|
-
</Trigger>
|
130
|
-
</Style.Triggers>
|
131
|
-
</Style>
|
132
|
-
</Label.Style>
|
133
|
-
</Label>
|
134
|
-
|
135
|
-
<!-- DataTrigger -->
|
136
|
-
<Label x:Name="label4" Content="{Binding DeviceId}">
|
137
|
-
<Label.Style>
|
138
|
-
<Style TargetType="{x:Type Label}">
|
139
|
-
<Setter Property="Background" Value="#FF24E827" />
|
140
|
-
<Style.Triggers>
|
141
|
-
<DataTrigger Binding="{Binding DeviceId}" Value="debaisu">
|
142
|
-
<Setter Property="Background" Value="#FFF39915" />
|
143
|
-
</DataTrigger>
|
144
|
-
</Style.Triggers>
|
145
|
-
</Style>
|
146
|
-
</Label.Style>
|
147
|
-
</Label>
|
148
|
-
|
149
|
-
<!-- Converter -->
|
150
|
-
<Label
|
151
|
-
x:Name="label5"
|
152
|
-
Background="{Binding DeviceId, Converter={StaticResource DeviceId2BrushConverter}, ConverterParameter='debaisu'}"
|
153
|
-
Content="{Binding DeviceId}" />
|
154
|
-
|
155
|
-
<!--#endregion-->
|
156
|
-
</StackPanel>
|
157
|
-
</UserControl>
|
158
|
-
```
|
159
|
-
```cs:UserControl1.xaml.cs
|
160
|
-
using System;
|
161
|
-
using System.Globalization;
|
162
|
-
using System.Windows.Controls;
|
163
|
-
using System.Windows.Data;
|
164
|
-
using System.Windows.Media;
|
165
|
-
|
166
|
-
namespace Q21wjdsxlvpvelo
|
167
|
-
{
|
168
|
-
// ValueConverter xamlでバインド時に相互変換に使用(BrushConverterとはちょっと違う)
|
169
|
-
// 今回は文字列(DeviceId)からブラシへ変換するOneWayのコンバーター
|
170
|
-
// [方法: バインドされたデータを変換する - WPF .NET Framework | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/desktop/wpf/data/how-to-convert-bound-data)
|
171
|
-
public class DeviceId2BrushConverter : IValueConverter
|
172
|
-
{
|
173
|
-
// ブラシをC#コードで用意してもいいのだが、xamlで書くほうが短い&複数パターン使い分け可能
|
174
|
-
//public Brush Ok { get; set; } = (Brush)new BrushConverter().ConvertFrom("#FFF39915");
|
175
|
-
public Brush Ok { get; set; }
|
176
|
-
public Brush Ng { get; set; }
|
177
|
-
|
178
|
-
// "debaisu"をプロパティで持ってもいいしparameterで渡してもいいし
|
179
|
-
//public string Value { get; set; }
|
180
|
-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
181
|
-
{
|
182
|
-
//return value?.Equals(Value) == true ? Ok : Ng;
|
183
|
-
return value?.Equals(parameter) == true ? Ok : Ng;
|
184
|
-
}
|
185
|
-
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
186
|
-
=> throw new NotImplementedException();
|
187
|
-
}
|
188
|
-
|
189
|
-
|
190
|
-
public partial class UserControl1 : UserControl
|
191
|
-
{
|
192
|
-
private static BrushConverter brushConverter = new BrushConverter();
|
193
|
-
|
194
|
-
public UserControl1()
|
195
|
-
{
|
196
|
-
InitializeComponent();
|
197
|
-
|
198
|
-
//DataContext = hoge;
|
199
|
-
// 明示的に変更しなければDataContextは親の要素から引き継ぎ
|
200
|
-
// Window→StackPanel→UserControl1→StackPanel→Label
|
201
|
-
}
|
202
|
-
|
203
|
-
// MainWindowから呼ばれて変える(label2)
|
204
|
-
public void ChangeText(string deviceId)
|
205
|
-
{
|
206
|
-
label2.Content = deviceId;
|
207
|
-
switch (deviceId)
|
208
|
-
{
|
209
|
-
case "debaisu":
|
210
|
-
label2.Background = (Brush)brushConverter.ConvertFrom("#FFF39915");
|
211
|
-
break;
|
212
|
-
default:
|
213
|
-
label2.Background = (Brush)brushConverter.ConvertFrom("#FF24E827");
|
214
52
|
break;
|
215
53
|
}
|
216
54
|
}
|
@@ -218,7 +56,127 @@
|
|
218
56
|
}
|
219
57
|
```
|
220
58
|
|
59
|
+
```xml:UserControl1.xaml
|
60
|
+
<UserControl
|
221
|
-
|
61
|
+
x:Class="●●●●●●●.UserControl1"
|
222
|
-
|
62
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
63
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
64
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
65
|
+
xmlns:local="clr-namespace:●●●●●●●"
|
66
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
67
|
+
d:DesignHeight="450"
|
68
|
+
d:DesignWidth="800"
|
69
|
+
mc:Ignorable="d">
|
70
|
+
<UserControl.Resources>
|
223
71
|
|
72
|
+
<!-- ContentがOKだったらBackgroundがオレンジ OK以外だったら緑になるLabelのStyle -->
|
73
|
+
<Style x:Key="OkNgLabelStyle" TargetType="{x:Type Label}">
|
74
|
+
<!-- 基本的には緑 -->
|
75
|
+
<Setter Property="Background" Value="#FF24E827" />
|
76
|
+
<Style.Triggers>
|
77
|
+
<!-- ContentがOKだったら... -->
|
78
|
+
<Trigger Property="Content" Value="OK">
|
79
|
+
<!-- 背景をオレンジに変える -->
|
80
|
+
<Setter Property="Background" Value="#FFF39915" />
|
81
|
+
</Trigger>
|
82
|
+
</Style.Triggers>
|
83
|
+
</Style>
|
84
|
+
</UserControl.Resources>
|
85
|
+
|
86
|
+
<Grid>
|
87
|
+
<Label
|
88
|
+
x:Name="Device_lb"
|
89
|
+
Height="26"
|
90
|
+
Margin="30,150,0,0"
|
91
|
+
HorizontalAlignment="Left"
|
92
|
+
VerticalAlignment="Top"
|
93
|
+
Content="デバイス名"
|
94
|
+
FontSize="10" />
|
95
|
+
|
96
|
+
<TextBlock
|
97
|
+
x:Name="DeviceID_tb"
|
98
|
+
Margin="160,150,0,0"
|
99
|
+
HorizontalAlignment="Left"
|
100
|
+
VerticalAlignment="Top"
|
101
|
+
Text="仮 DeviceID"
|
102
|
+
TextWrapping="Wrap" />
|
103
|
+
|
104
|
+
<Label
|
105
|
+
x:Name="DeviceOk_lb"
|
106
|
+
Height="26"
|
107
|
+
Margin="300,150,0,0"
|
108
|
+
HorizontalAlignment="Left"
|
109
|
+
VerticalAlignment="Top"
|
110
|
+
Background="#FFF39915"
|
111
|
+
Content="仮 OK"
|
112
|
+
FontSize="10" />
|
113
|
+
|
114
|
+
<Label
|
115
|
+
x:Name="DeviceOk_lb2"
|
116
|
+
Height="26"
|
117
|
+
Margin="360,150,0,0"
|
118
|
+
HorizontalAlignment="Left"
|
119
|
+
VerticalAlignment="Top"
|
120
|
+
Content="仮 OK"
|
121
|
+
FontSize="10"
|
122
|
+
Style="{StaticResource OkNgLabelStyle}" />
|
123
|
+
</Grid>
|
124
|
+
</UserControl>
|
125
|
+
```
|
126
|
+
|
127
|
+
```cs:UserControl1.xaml.cs
|
128
|
+
using System;
|
129
|
+
using System.Collections.Generic;
|
130
|
+
using System.Linq;
|
131
|
+
using System.Text;
|
132
|
+
using System.Threading.Tasks;
|
133
|
+
using System.Windows;
|
134
|
+
using System.Windows.Controls;
|
135
|
+
using System.Windows.Data;
|
136
|
+
using System.Windows.Documents;
|
137
|
+
using System.Windows.Input;
|
138
|
+
using System.Windows.Media;
|
139
|
+
using System.Windows.Media.Imaging;
|
140
|
+
using System.Windows.Navigation;
|
141
|
+
using System.Windows.Shapes;
|
142
|
+
|
143
|
+
namespace ●●●●●●●
|
144
|
+
{
|
145
|
+
/// <summary>
|
146
|
+
/// UserControl1.xaml の相互作用ロジック
|
147
|
+
/// </summary>
|
148
|
+
public partial class UserControl1 : UserControl
|
149
|
+
{
|
150
|
+
public UserControl1()
|
151
|
+
{
|
152
|
+
InitializeComponent();
|
153
|
+
|
154
|
+
// もしかしたらここに書けるのかもしれない(A次第)
|
155
|
+
|
156
|
+
//string DeviceID = A.DeviceId;
|
157
|
+
//DeviceID_tb.Text = DeviceID;
|
158
|
+
|
159
|
+
//switch (DeviceID)
|
160
|
+
//{
|
161
|
+
// case "debaisu":
|
224
|
-
|
162
|
+
// DeviceOk_lb.Background = (Brush)new BrushConverter().ConvertFrom("#FFF39915");
|
163
|
+
// DeviceOk_lb.Content = "OK";
|
164
|
+
|
165
|
+
// DeviceOk_lb2.Content = "OK";
|
166
|
+
// break;
|
167
|
+
// default:
|
168
|
+
// DeviceOk_lb.Background = (Brush)new BrushConverter().ConvertFrom("#FF24E827");
|
169
|
+
// DeviceOk_lb.Content = "NG";
|
170
|
+
|
171
|
+
// DeviceOk_lb2.Content = "NG";
|
172
|
+
// break;
|
173
|
+
//}
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
```
|
178
|
+
|
179
|
+
---
|
180
|
+
|
181
|
+
`namespace`(プロジェクト名)や`x:Name`等をそのまま公開するのは支障がある場合、**新たなプロジェクト**を作って当たり障りのない名前(hoge,fuga・foo,bar・aaa,bbb等)にしてください。
|
182
|
+
**今の**プロジェクトを編集して出すのは、編集忘れがあったり矛盾したりしてかえって混乱します。
|