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

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

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

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

WPF

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

Q&A

解決済

1回答

3418閲覧

WPF カレンダーコントロールで任意の日の背景色と文字色を変更したい

WBD212

総合スコア5

C#

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

WPF

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

1グッド

0クリップ

投稿2020/12/24 07:40

カレンダーコントロールの任意の日の背景色と文字色を変更したい

ユーザ操作により祝日を設定する画面の実装を行っております。
祝日設定された日の「背景色を赤」「文字色を白」に変更したいのですがうまくいっておりません。

発生している問題

問題1

下記ページを参考に文字色を変更することは出来ましたが、背景色を変更することが出来ません。
「MainWindow.xaml.cs」の23行目で元々のForeground設定を無効化しているように
Background設定を無効化する必要があるという推測はあるのですが具体的な記述方法がわかりません。
https://iyemon018.hatenablog.com/entry/2016/05/10/231206

問題2

ページロード時のみ適用されておりその後のイベントで祝日設定を増やしても反映されない状態です。
追加ボタン押下時にテキストボックスに入力された日の背景色と文字色が変わるようにしたいです。

該当のソースコード

MainWindow.xaml

xaml

1<Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 xmlns:con="clr-namespace:WpfApp1" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="450" Width="800"> 10 <Window.Resources> 11 <local:BrushConverter x:Key="BackGroundConverter" /> 12 <local:BrushConverter x:Key="BrushConverter" /> 13 <Style x:Key="DefaultCalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}"> 14 <Setter Property="Background" Value="{Binding Converter={StaticResource BackGroundConverter}, Mode=OneWay}" /> 15 <Setter Property="Foreground" Value="{Binding Converter={StaticResource BrushConverter}, Mode=OneWay}" /> 16 <Setter Property="Template"> 17 <Setter.Value> 18 <ControlTemplate TargetType="{x:Type CalendarDayButton}"> 19 <ContentPresenter x:Name="NormalText" 20 Margin="5,1,5,1" 21 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 22 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 23 <!--TextElement.Foreground="#FF333333"/>--> 24 </ControlTemplate> 25 </Setter.Value> 26 </Setter> 27 </Style> 28 </Window.Resources> 29 <StackPanel Orientation="Horizontal"> 30 <Calendar x:Name="custom" CalendarDayButtonStyle="{DynamicResource DefaultCalendarDayButtonStyle}" /> 31 <TextBox x:Name="txt" Width="60" Height="40"/> 32 <Button Click="btn_Click" Content="追加" Width="60" Height="40"/> 33 </StackPanel> 34</Window>

MainWindow.xaml.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Windows; 4 5namespace WpfApp1 6{ 7 /// <summary> 8 /// MainWindow.xaml の相互作用ロジック 9 /// </summary> 10 public partial class MainWindow : Window 11 { 12 public MainWindow() 13 { 14 // DBから取得したてい 15 list.Add(new DateTime(2020, 12, 24)); 16 list.Add(new DateTime(2020, 12, 25)); 17 } 18 19 static public List<DateTime> list = new List<DateTime>(); 20 21 private void btn_Click(object sender, RoutedEventArgs e) 22 { 23 try 24 { 25 var date = DateTime.ParseExact(txt.Text, "yyyyMMdd", null); 26 list.Add(date); 27 } 28 catch 29 { 30 MessageBox.Show("yyyyMMdd形式で入力してください"); 31 } 32 } 33 } 34} 35

BackGroundConverter.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7namespace WpfApp1 8{ 9 using System; 10 using System.Globalization; 11 using System.Windows; 12 using System.Windows.Data; 13 using System.Windows.Media; 14 15 /// <summary> 16 /// <see cref="T:DateTime"/>の値から曜日の<see cref="T:Brush"/>リソースへのコンバータークラスです。 17 /// </summary> 18 /// <seealso cref="System.Windows.Data.IValueConverter" /> 19 [ValueConversion(typeof(DateTime), typeof(Brush))] 20 public class BackGroundConverter : IValueConverter 21 { 22 /// <summary>値を変換します。</summary> 23 /// <returns>変換された値。 メソッドが null を返す場合は、有効な null 値が使用されています。</returns> 24 /// <param name="value">バインディング ソースによって生成された値。</param> 25 /// <param name="targetType">バインディング ターゲット プロパティの型。</param> 26 /// <param name="parameter">使用するコンバーター パラメーター。</param> 27 /// <param name="culture">コンバーターで使用するカルチャ。</param> 28 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 29 { 30 if (!(value is DateTime)) 31 { 32 return DependencyProperty.UnsetValue; 33 } 34 35 return MainWindow.list.Any(x => x == (DateTime)value) ? Brushes.Red : DependencyProperty.UnsetValue; 36 } 37 38 /// <summary>値を変換します。</summary> 39 /// <returns>変換された値。 メソッドが null を返す場合は、有効な null 値が使用されています。</returns> 40 /// <param name="value">バインディング ターゲットによって生成される値。</param> 41 /// <param name="targetType">変換後の型。</param> 42 /// <param name="parameter">使用するコンバーター パラメーター。</param> 43 /// <param name="culture">コンバーターで使用するカルチャ。</param> 44 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 45 { 46 throw new NotImplementedException(); 47 } 48 } 49} 50

BrushConverter.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7namespace WpfApp1 8{ 9 using System; 10 using System.Globalization; 11 using System.Windows; 12 using System.Windows.Data; 13 using System.Windows.Media; 14 15 /// <summary> 16 /// <see cref="T:DateTime"/>の値から曜日の<see cref="T:Brush"/>リソースへのコンバータークラスです。 17 /// </summary> 18 /// <seealso cref="System.Windows.Data.IValueConverter" /> 19 [ValueConversion(typeof(DateTime), typeof(Brush))] 20 public class BrushConverter : IValueConverter 21 { 22 /// <summary>値を変換します。</summary> 23 /// <returns>変換された値。 メソッドが null を返す場合は、有効な null 値が使用されています。</returns> 24 /// <param name="value">バインディング ソースによって生成された値。</param> 25 /// <param name="targetType">バインディング ターゲット プロパティの型。</param> 26 /// <param name="parameter">使用するコンバーター パラメーター。</param> 27 /// <param name="culture">コンバーターで使用するカルチャ。</param> 28 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 29 { 30 if (!(value is DateTime)) 31 { 32 return DependencyProperty.UnsetValue; 33 } 34 35 return MainWindow.list.Any(x => x == (DateTime)value) ? Brushes.White : DependencyProperty.UnsetValue; 36 } 37 38 /// <summary>値を変換します。</summary> 39 /// <returns>変換された値。 メソッドが null を返す場合は、有効な null 値が使用されています。</returns> 40 /// <param name="value">バインディング ターゲットによって生成される値。</param> 41 /// <param name="targetType">変換後の型。</param> 42 /// <param name="parameter">使用するコンバーター パラメーター。</param> 43 /// <param name="culture">コンバーターで使用するカルチャ。</param> 44 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 45 { 46 throw new NotImplementedException(); 47 } 48 } 49} 50
TN8001👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

問題1

まずBackGroundConverterの型が違います。

xml

1<!--<local:BrushConverter x:Key="BackGroundConverter" />--> 2<local:BackGroundConverter x:Key="BackGroundConverter" />

なんでもいいですが、なにかしらに{TemplateBinding Background}します。

xml

1<Style x:Key="DefaultCalendarDayButtonStyle" TargetType="{x:Type CalendarDayButton}"> 2 <Setter Property="Background" Value="{Binding Converter={StaticResource BackGroundConverter}, Mode=OneWay}" /> 3 <Setter Property="Foreground" Value="{Binding Converter={StaticResource BrushConverter}, Mode=OneWay}" /> 4 <Setter Property="Template"> 5 <Setter.Value> 6 <ControlTemplate TargetType="{x:Type CalendarDayButton}"> 7 <Border Background="{TemplateBinding Background}"> 8 <ContentPresenter 9 x:Name="NormalText" 10 Margin="5,1,5,1" 11 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 12 VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 13 </Border> 14 </ControlTemplate> 15 </Setter.Value> 16 </Setter> 17</Style>

問題2

ページ(月)を変えると反映されるようなので、強制的に切り替えてみました。

特にちらつき等はありませんが、月ビュー?になっていないと戻った時に変わっていませんでした。
ちゃんとやるなら月ビュー?にしたうえで切り替えて、さらにビューを戻す等?

cs

1var date = DateTime.ParseExact(txt.Text, "yyyyMMdd", null); 2list.Add(date); 3 4var d = custom.DisplayDate; 5custom.DisplayDate = new DateTime(); 6custom.DisplayDate = d;

投稿2020/12/24 10:35

編集2023/07/25 15:25
TN8001

総合スコア9357

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

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

WBD212

2020/12/25 00:58 編集

ご教授いただいた方法で背景色と文字色の両方の変更が出来ました。 ありがとうございます。 月ビューではないと変更が反映されない件に関しては少し不格好ですが下記コードにて対応しました。 ```C# var date = DateTime.ParseExact(txt.Text, "yyyyMMdd", null); list.Add(date); var d = custom.DisplayDate; var mode = custom.DisplayMode; custom.DisplayMode = System.Windows.Controls.CalendarMode.Month; custom.DisplayDate = new DateTime(); custom.DisplayDate = d; custom.DisplayMode = mode; ```
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問