質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

1492閲覧

チェックボックスの状態によって、コンボボックスのリスト内の文字の色を変えたい。

Yamato.dd

総合スコア48

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2023/01/12 02:20

編集2023/01/12 04:10

前提

開発ツール:Visual Studio 2019
開発言語:C#、WPF(.Net Framework 4.7)
OS:Windows 10

実現したいこと

チェックボックスの状態によって、コンボボックスのリスト内の文字の色を変更したいです。

■例
フォームの中にコンボボックスが1つ、チェックボックスが2つあるとします。
コンボボックスの中には2つのアイテム(仮に"アイテム1"と"アイテム2"とする)があります。
チェックボックスのラベルが、それぞれ「Check1」、「Check2」となっているとします。

 コンボボックスを展開してリストを表示させたときに、Check1にチェックが入っていたら、
「アイテム1」の文字を赤く、Check2にチェックが入っていたら「アイテム2」の文字を赤く、
チェックが入っていなければ、文字の色を黒くしたいです。

まずはこのようなことが実現できるのかが知りたいです。

ソースコード

C#

1// sample.xaml 2<Window x:Class="Sample.MainWindow" 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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 xmlns:local="clr-namespace:Sample" 8 mc:Ignorable="d" 9 Title="Sample" Height="150" Width="400"> 10 <Grid> 11 <CheckBox Name="Check1" Content="Check1" HorizontalAlignment="Left" Height="21" Margin="250,20,0,0" VerticalAlignment="Top" Width="117"/> 12 <CheckBox Name="Check2" Content="Check2" HorizontalAlignment="Left" Height="21" Margin="250,40,0,0" VerticalAlignment="Top" Width="117"/> 13 <ComboBox Name="Combo1" HorizontalAlignment="Left" Margin="28,19,0,0" VerticalAlignment="Top" Width="120"> 14 <ComboBoxItem Content="アイテム1"/> 15 <ComboBoxItem Content="アイテム2"/> 16 </ComboBox> 17 </Grid> 18</Window>

宜しくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Yamato.dd

2023/01/12 04:12

ご指摘ありがとうございます。 ソースコードを追加しました。
退会済みユーザー

退会済みユーザー

2023/01/12 05:32

どこで躓いていて、何が分かれば解決できるのですか?
退会済みユーザー

退会済みユーザー

2023/01/13 00:11

質問者さん、その後無言ですが、回答が出ているのでそれらに対するフィードバックを返してください。役に立った/立たなかったぐらいはすぐに返せるのでは? 役に立たなかったならどこがダメかを書くとより期待に近い回答が出てくるかも。解決したなら解決に役立った回答にベストアンサーをつけてクローズしてください。とにかく無言は NG です。
Yamato.dd

2023/01/13 00:58

すみません。返信が遅くなってしまいました。 色々と立て込んでいて、確認できていませんでした。 「何が分かれば解決できるのか?」についてですが、実現する方法の見当すらつかなかったもので。
guest

回答2

0

チェックボックスの状態によって、コンボボックスのリスト内の文字の色を変更したいです。

いろいろ方法はあるでしょうが、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

1using System; 2using System.Collections.Generic; 3using System.Globalization; 4using System.Windows; 5using System.Windows.Data; 6using System.Windows.Media; 7using CommunityToolkit.Mvvm.ComponentModel; 8 9 10namespace 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

アプリ画像

投稿2023/01/12 08:57

TN8001

総合スコア9326

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Yamato.dd

2023/01/13 00:59

ご回答頂きありがとうございます。 参考にさせていただきます。
guest

0

ベストアンサー

Check1にチェックが入っていたら、「アイテム1」の文字を赤く、Check2にチェックが入っていたら「アイテム2」の文字を赤く、チェックが入っていなければ、文字の色を黒くしたいです。

・・・のところだけに限ってレスしておきます。上の質問のコメント欄で聞いた「どこで躓いていて、何が分かれば解決できるのですか?」に返事がないので、ホントにこれで望む答えになっているかは分かりませんが。

CheckBox.Click イベントのハンドラを使います。デザイン画面で以下のようイベントハンドラのコードの枠組みを MainWindow.xaml.cs に生成し、

イメージ説明

それに以下のようなコードを書いて、CheckBox のチェックの有無によって、当該 ComboBoxItem の Foreground プロパティの設定を変えれば、

C#

1private void Check1_Click(object sender, RoutedEventArgs e) 2{ 3 if (((CheckBox)sender).IsChecked == true) 4 { 5 ((ComboBoxItem)Combo1.Items[0]).Foreground = Brushes.Red; 6 } 7 else 8 { 9 ((ComboBoxItem)Combo1.Items[0]).Foreground = Brushes.Black; 10 } 11} 12 13private void Check2_Click(object sender, RoutedEventArgs e) 14{ 15 if (((CheckBox)sender).IsChecked == true) 16 { 17 ((ComboBoxItem)Combo1.Items[1]).Foreground = Brushes.Red; 18 } 19 else 20 { 21 ((ComboBoxItem)Combo1.Items[1]).Foreground = Brushes.Black; 22 } 23}

以下のように色は変わります。

イメージ説明

投稿2023/01/12 07:55

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Yamato.dd

2023/01/13 01:01

重ね重ねありがとうございます。 おかげさまで実現することができました。 こちらをベストアンサーとさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問