Script
でUserControl1
を作っているなら、そこで直接変えられます(エスパー回答通り)
もしかしたらUserControl1
内で、A
を使えるのかもしれません(A
次第)
グリッド直置きはよくないですが、変更点が多いと混乱させそうなので目をつぶります^^;
cs:Script.cs
1 using System ;
2 using System . Linq ;
3 using System . Text ;
4 using System . Windows ;
5 using System . Windows . Controls ;
6 using System . Collections . Generic ;
7 using System . Reflection ;
8 using System . Runtime . CompilerServices ;
9 using System . Windows . Media ;
10 using ●●●●●●●●●●●●●●●● ;
11 using ●●●●●●●●●●●●●●●● ;
12
13 namespace ●●●●●●●
14 {
15 public class Script
16 {
17 public Script ( )
18 {
19 }
20
21 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
22 public void Execute ( ●〇〇〇〇〇〇●〇〇〇 〇〇〇〇〇〇 , System . Windows . Window window )
23 {
24 var control = new UserControl1 ( ) ;
25 window . Content = control ;
26 window . Height = 842 ;
27 window . Width = 595 ;
28
29 string DeviceID = A . DeviceId ;
30 control . DeviceID_tb . Text = DeviceID ;
31
32 switch ( DeviceID )
33 {
34 case "debaisu" :
35 // エスパー回答するならUserControlにx:Nameを付けて、userControl.ラベル名_lb.Background...とメイン側で変える。
36 control . DeviceOk_lb . Background = ( Brush ) new BrushConverter ( ) . ConvertFrom ( "#FFF39915" ) ;
37 control . DeviceOk_lb . Content = "OK" ;
38
39 // OK・NG 2択で色も共通ならTriggerで色を切り替えるStyleを作っておくと、Contentを変えるだけで色も変わって楽
40 control . DeviceOk_lb2 . Content = "OK" ;
41 break ;
42 default :
43 control . DeviceOk_lb . Background = ( Brush ) new BrushConverter ( ) . ConvertFrom ( "#FF24E827" ) ;
44 control . DeviceOk_lb . Content = "NG" ;
45
46 control . DeviceOk_lb2 . Content = "NG" ;
47 break ;
48 }
49 }
50 }
51 }
xml:UserControl1.xaml
1 < UserControl
2 x: Class = " ●●●●●●●.UserControl1 "
3 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
4 xmlns: x = " http://schemas.microsoft.com/winfx/2006/xaml "
5 xmlns: d = " http://schemas.microsoft.com/expression/blend/2008 "
6 xmlns: local = " clr-namespace:●●●●●●● "
7 xmlns: mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 "
8 d: DesignHeight = " 450 "
9 d: DesignWidth = " 800 "
10 mc: Ignorable = " d " >
11 < UserControl.Resources >
12
13 <!-- ContentがOKだったらBackgroundがオレンジ OK以外だったら緑になるLabelのStyle -->
14 < Style x: Key = " OkNgLabelStyle " TargetType = " {x:Type Label} " >
15 <!-- 基本的には緑 -->
16 < Setter Property = " Background " Value = " #FF24E827 " />
17 < Style.Triggers >
18 <!-- ContentがOKだったら... -->
19 < Trigger Property = " Content " Value = " OK " >
20 <!-- 背景をオレンジに変える -->
21 < Setter Property = " Background " Value = " #FFF39915 " />
22 </ Trigger >
23 </ Style.Triggers >
24 </ Style >
25 </ UserControl.Resources >
26
27 < Grid >
28 < Label
29 x: Name = " Device_lb "
30 Height = " 26 "
31 Margin = " 30,150,0,0 "
32 HorizontalAlignment = " Left "
33 VerticalAlignment = " Top "
34 Content = " デバイス名 "
35 FontSize = " 10 " />
36
37 < TextBlock
38 x: Name = " DeviceID_tb "
39 Margin = " 160,150,0,0 "
40 HorizontalAlignment = " Left "
41 VerticalAlignment = " Top "
42 Text = " 仮 DeviceID "
43 TextWrapping = " Wrap " />
44
45 < Label
46 x: Name = " DeviceOk_lb "
47 Height = " 26 "
48 Margin = " 300,150,0,0 "
49 HorizontalAlignment = " Left "
50 VerticalAlignment = " Top "
51 Background = " #FFF39915 "
52 Content = " 仮 OK "
53 FontSize = " 10 " />
54
55 < Label
56 x: Name = " DeviceOk_lb2 "
57 Height = " 26 "
58 Margin = " 360,150,0,0 "
59 HorizontalAlignment = " Left "
60 VerticalAlignment = " Top "
61 Content = " 仮 OK "
62 FontSize = " 10 "
63 Style = " {StaticResource OkNgLabelStyle} " />
64 </ Grid >
65 </ UserControl >
cs:UserControl1.xaml.cs
1 using System ;
2 using System . Collections . Generic ;
3 using System . Linq ;
4 using System . Text ;
5 using System . Threading . Tasks ;
6 using System . Windows ;
7 using System . Windows . Controls ;
8 using System . Windows . Data ;
9 using System . Windows . Documents ;
10 using System . Windows . Input ;
11 using System . Windows . Media ;
12 using System . Windows . Media . Imaging ;
13 using System . Windows . Navigation ;
14 using System . Windows . Shapes ;
15
16 namespace ●●●●●●●
17 {
18
19
20
21 public partial class UserControl1 : UserControl
22 {
23 public UserControl1 ( )
24 {
25 InitializeComponent ( ) ;
26
27 // もしかしたらここに書けるのかもしれない(A次第)
28
29 //string DeviceID = A.DeviceId;
30 //DeviceID_tb.Text = DeviceID;
31
32 //switch (DeviceID)
33 //{
34 // case "debaisu":
35 // DeviceOk_lb.Background = (Brush)new BrushConverter().ConvertFrom("#FFF39915");
36 // DeviceOk_lb.Content = "OK";
37
38 // DeviceOk_lb2.Content = "OK";
39 // break;
40 // default:
41 // DeviceOk_lb.Background = (Brush)new BrushConverter().ConvertFrom("#FF24E827");
42 // DeviceOk_lb.Content = "NG";
43
44 // DeviceOk_lb2.Content = "NG";
45 // break;
46 //}
47 }
48 }
49 }
namespace
(プロジェクト名)やx:Name
等をそのまま公開するのは支障がある場合、新たなプロジェクト を作って当たり障りのない名前(hoge,fuga・foo,bar・aaa,bbb等)にしてください。
今の プロジェクトを編集して出すのは、編集忘れがあったり矛盾したりしてかえって混乱します。