チェックボックスの状態によって、コンボボックスのリスト内の文字の色を変更したいです。
いろいろ方法はあるでしょうが、ItemsSource有無×コンバータ有無の4例。
xml
1 < Window
2 x: Class = " Q1hyy440mlba7px.MainWindow "
3 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
4 xmlns: x = " http://schemas.microsoft.com/winfx/2006/xaml "
5 xmlns: local = " clr-namespace:Q1hyy440mlba7px "
6 Width = " 800 "
7 Height = " 450 "
8 DataContext = " {Binding RelativeSource={RelativeSource Self}} " >
9 < Window.Resources >
10 < local: Bool2BrushConverter x: Key = " Bool2BrushConverter " />
11 </ Window.Resources >
12 < UniformGrid >
13
14 < GroupBox Header = " ベタ置き " >
15 < StackPanel >
16 < CheckBox x: Name = " Check1 " Content = " Check1 " />
17 < CheckBox x: Name = " Check2 " Content = " Check2 " />
18 < ComboBox >
19 < ComboBoxItem Content = " アイテム1 " >
20 < ComboBoxItem.Style >
21 < Style TargetType = " ComboBoxItem " >
22 < Style.Triggers >
23 < DataTrigger Binding = " {Binding IsChecked, ElementName=Check1} " Value = " True " >
24 < Setter Property = " Foreground " Value = " Red " />
25 </ DataTrigger >
26 </ Style.Triggers >
27 </ Style >
28 </ ComboBoxItem.Style >
29 </ ComboBoxItem >
30 < ComboBoxItem Content = " アイテム2 " >
31 < ComboBoxItem.Style >
32 < Style TargetType = " ComboBoxItem " >
33 < Style.Triggers >
34 < DataTrigger Binding = " {Binding IsChecked, ElementName=Check2} " Value = " True " >
35 < Setter Property = " Foreground " Value = " Red " />
36 </ DataTrigger >
37 </ Style.Triggers >
38 </ Style >
39 </ ComboBoxItem.Style >
40 </ ComboBoxItem >
41 </ ComboBox >
42 </ StackPanel >
43 </ GroupBox >
44
45
46 < GroupBox Header = " ItemsSource " >
47 < StackPanel >
48 < CheckBox Content = " Check1 " IsChecked = " {Binding Items[0].IsEmphasized} " />
49 < CheckBox Content = " Check2 " IsChecked = " {Binding Items[1].IsEmphasized} " />
50 < ComboBox DisplayMemberPath = " Name " ItemsSource = " {Binding Items} " >
51 < ComboBox.ItemContainerStyle >
52 < Style TargetType = " ComboBoxItem " >
53 < Style.Triggers >
54 < DataTrigger Binding = " {Binding IsEmphasized} " Value = " True " >
55 < Setter Property = " Foreground " Value = " Red " />
56 </ DataTrigger >
57 </ Style.Triggers >
58 </ Style >
59 </ ComboBox.ItemContainerStyle >
60 <!-- これは選択後の表示も赤くなる(DisplayMemberPathを外すこと) -->
61 <!--<ComboBox.ItemTemplate>
62 <DataTemplate>
63 <TextBlock Text="{Binding Name}">
64 <TextBlock.Style>
65 <Style TargetType="TextBlock">
66 <Style.Triggers>
67 <DataTrigger Binding="{Binding IsEmphasized}" Value="True">
68 <Setter Property="Foreground" Value="Red" />
69 </DataTrigger>
70 </Style.Triggers>
71 </Style>
72 </TextBlock.Style>
73 </TextBlock>
74 </DataTemplate>
75 </ComboBox.ItemTemplate>-->
76 </ ComboBox >
77 </ StackPanel >
78 </ GroupBox >
79
80
81 < GroupBox Header = " ベタ置き&コンバータ " >
82 < StackPanel >
83 < CheckBox x: Name = " Check11 " Content = " Check1 " />
84 < CheckBox x: Name = " Check22 " Content = " Check2 " />
85 < ComboBox >
86 < ComboBoxItem Content = " アイテム1 " Foreground = " {Binding IsChecked, Converter={StaticResource Bool2BrushConverter}, ElementName=Check11} " />
87 < ComboBoxItem Content = " アイテム2 " Foreground = " {Binding IsChecked, Converter={StaticResource Bool2BrushConverter}, ElementName=Check22} " />
88 </ ComboBox >
89 </ StackPanel >
90 </ GroupBox >
91
92
93 < GroupBox Header = " ItemsSource&コンバータ " >
94 < StackPanel >
95 < CheckBox Content = " Check1 " IsChecked = " {Binding Items[0].IsEmphasized} " />
96 < CheckBox Content = " Check2 " IsChecked = " {Binding Items[1].IsEmphasized} " />
97 < ComboBox DisplayMemberPath = " Name " ItemsSource = " {Binding Items} " >
98 < ComboBox.ItemContainerStyle >
99 < Style TargetType = " ComboBoxItem " >
100 < Setter Property = " Foreground " Value = " {Binding IsEmphasized, Converter={StaticResource Bool2BrushConverter}} " />
101 </ Style >
102 </ ComboBox.ItemContainerStyle >
103 <!-- これは選択後の表示も赤くなる(DisplayMemberPathを外すこと) -->
104 <!--<ComboBox.ItemTemplate>
105 <DataTemplate>
106 <TextBlock Foreground="{Binding IsEmphasized, Converter={StaticResource Bool2BrushConverter}}" Text="{Binding Name}" />
107 </DataTemplate>
108 </ComboBox.ItemTemplate>-->
109 </ ComboBox >
110 </ StackPanel >
111 </ GroupBox >
112 </ UniformGrid >
113 </ Window >
cs
1 using System ;
2 using System . Collections . Generic ;
3 using System . Globalization ;
4 using System . Windows ;
5 using System . Windows . Data ;
6 using System . Windows . Media ;
7 using CommunityToolkit . Mvvm . ComponentModel ;
8
9
10 namespace Q1hyy440mlba7px
11 {
12 // [ObservableObject - .NET Community Toolkit | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/communitytoolkit/mvvm/observableobject)
13 public class Item : ObservableObject
14 {
15 public string Name { get ; }
16
17 public bool IsEmphasized { get => _IsEmphasized ; set => SetProperty ( ref _IsEmphasized , value ) ; }
18 private bool _IsEmphasized ;
19
20 public Item ( string name ) => Name = name ;
21 }
22
23 public class Bool2BrushConverter : IValueConverter
24 {
25 public object Convert ( object value , Type targetType , object parameter , CultureInfo culture )
26 {
27 if ( value is bool b && b ) return Brushes . Red ;
28 return Brushes . Black ;
29 }
30 public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture ) => throw new NotImplementedException ( ) ;
31 }
32
33 public partial class MainWindow : Window
34 {
35 public List < Item > Items { get ; } = new List < Item >
36 {
37 new Item ( "アイテム1" ) ,
38 new Item ( "アイテム2" ) ,
39 } ;
40
41 public MainWindow ( ) => InitializeComponent ( ) ;
42 }
43 }
NuGet Gallery | CommunityToolkit.Mvvm 8.0.0
MVVM Toolkit の概要 - .NET Community Toolkit | Microsoft Learn