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

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

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

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

Visual Studio

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

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

WPF

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

Q&A

解決済

1回答

2766閲覧

WPFのStyleのDataTriggerについて

arw.tyx-out_mz

総合スコア27

C#

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

Visual Studio

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

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

WPF

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

0グッド

3クリップ

投稿2019/06/16 13:34

編集2019/06/16 13:36

前提・実現したいこと

WPFのStyleのDataTriggerについて,ある特定のプロパティの値が何かによって文字の色を変えたいと思っています.
再利用性を高めるために,そのスタイルを適用する際に,どの値と比較するのかを動的に設定できるようにしたいのですが,なにかいい方法はありますでしょうか.

該当のソースコード

Xaml

1 <Style x:Key="LabelStyle" TargetType="{x:Type Label}"> 2 <Setter Property="VerticalAlignment" Value="Center"/> 3 <Setter Property="Foreground" Value="Black"/> 4 <Setter Property="FontSize" Value="22" /> 5 6 <Style.Triggers> 7 <!-- ここのValueの値をパラメータ化して,使う場所で動的に指定したい --> 8 <DataTrigger Binding="{Binding SetupState}" Value="1"> 9 <Setter Property="Foreground" Value="Red" /> 10 </DataTrigger> 11 </Style.Triggers> 12 </Style> 13 14 <Label Content="ここはSetupStateが1のときに文字を赤くしたい" Style="{StaticResource LabelStyle}" /> 15 <Label Content="ここはSetupStateが2のときに文字を赤くしたい" Style="{StaticResource LabelStyle}" /> 16 <Label Content="ここはSetupStateが3のときに文字を赤くしたい" Style="{StaticResource LabelStyle}" /> 17 <Button Command="{Binding InteractiveCommand}" /> 18

C#

1public class MainControlViewModel : BindableBase 2{ 3 4 int m_state; 5 public int SetupState 6 { 7 get { return m_state; } 8 set { SetProperty(ref m_state, value); } 9 } 10 11 public ReactiveCommand InteractiveCommand { get; } 12 13 public MainControlViewModel() 14 { 15 SetupState = 1; 16 InteractiveCommand = new ReactiveCommand(); 17 InteractiveCommand.Subscribe(_ => 18 { 19 SetupState++; 20 }); 21 } 22} 23

補足情報(FW/ツールのバージョンなど)

Visual Studio 2017 Community
.NET Framework 4.6.1
Prism v7.1.0.431

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

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

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

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

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

guest

回答1

0

ベストアンサー

値コンバーターを使ってみてはどうでしょうか。

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 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 <Window.DataContext> 10 <local:MainControlViewModel/> 11 </Window.DataContext> 12 <Window.Resources> 13 <local:SetupStateColorConverter 14 x:Key="SetupStateColorConverter" 15 NormalBrush="Black" 16 HighlightBrush="Red"/> 17 </Window.Resources> 18 <Grid> 19 <Grid.RowDefinitions> 20 <RowDefinition/> 21 <RowDefinition/> 22 <RowDefinition/> 23 <RowDefinition/> 24 </Grid.RowDefinitions> 25 <Label 26 Grid.Row="0" 27 Foreground="{Binding SetupState, Converter={StaticResource SetupStateColorConverter}, ConverterParameter=1}" 28 Content="ここはSetupStateが1のときに文字を赤くしたい" /> 29 <Label 30 Grid.Row="1" 31 Foreground="{Binding SetupState, Converter={StaticResource SetupStateColorConverter}, ConverterParameter=2}" 32 Content="ここはSetupStateが2のときに文字を赤くしたい" /> 33 <Label 34 Grid.Row="2" 35 Foreground="{Binding SetupState, Converter={StaticResource SetupStateColorConverter}, ConverterParameter=3}" 36 Content="ここはSetupStateが3のときに文字を赤くしたい" /> 37 <Button Grid.Row="3" Command="{Binding InteractiveCommand}" /> 38 </Grid> 39</Window> 40

SetupStateColorConverter.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Globalization; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7using System.Windows; 8using System.Windows.Data; 9using System.Windows.Media; 10 11namespace WpfApp1 12{ 13 public class SetupStateColorConverter : DependencyObject, IValueConverter 14 { 15 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 16 { 17 if (!(value is int intValue)) return NormalBrush; 18 if (!(parameter is string strParam)) return NormalBrush; 19 if (!(int.TryParse(strParam, out int intParam))) return NormalBrush; 20 if (intValue != intParam) return NormalBrush; 21 return HighlightBrush; 22 } 23 24 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 25 { 26 throw new NotImplementedException(); 27 } 28 29 #region NormalBrush 30 public Brush NormalBrush 31 { 32 get { return (Brush)GetValue(NormalBrushProperty); } 33 set { SetValue(NormalBrushProperty, value); } 34 } 35 36 public static readonly DependencyProperty NormalBrushProperty = 37 DependencyProperty.Register( 38 nameof(NormalBrush), 39 typeof(Brush), 40 typeof(SetupStateColorConverter), 41 new PropertyMetadata(new SolidColorBrush(Colors.Black))); 42 #endregion 43 44 #region HighlightBrush 45 public Brush HighlightBrush 46 { 47 get { return (Brush)GetValue(HighlightBrushProperty); } 48 set { SetValue(HighlightBrushProperty, value); } 49 } 50 51 public static readonly DependencyProperty HighlightBrushProperty = 52 DependencyProperty.Register( 53 nameof(HighlightBrush), 54 typeof(Brush), 55 typeof(SetupStateColorConverter), 56 new PropertyMetadata(new SolidColorBrush(Colors.Red))); 57 #endregion 58 } 59}

投稿2019/06/18 16:00

Zuishin

総合スコア28662

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

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

Zuishin

2019/06/18 23:43

今考えると添付ビヘイビアの方がいいかもしれませんね。
arw.tyx-out_mz

2019/06/19 11:22

Converterを使う方法は考えました. ただ,それに対しても似たようにConverterParameterを動的に変更できないかなと考えてしまいます. {Binding SetupState, Converter={StaticResource SetupStateColorConverter}, ConverterParameter=1}の中でConverterParameterを指定する場所までは同じ記述をしてるのでConverterParameter部分だけ動的にLabelの定義場所で渡してあげて,それまでの部分はStyleの中に含められないかなと....
Zuishin

2019/06/19 11:47 編集

では添付ビヘイビアかマークアップ拡張がいいのではないでしょうか。 添付ビヘイビア http://garicchi.hatenablog.jp/entry/2014/03/27/041455 http://sourcechord.hatenablog.com/entry/2014/03/15/171857 例えば Grid.Row のような添付プロパティ SetupState.Number を作り、そこに値を入れることでラベルのプロパティを書き換えるのが添付ビヘイビアです。 マークアップ拡張は {Binding ... } と書く代わりに {SetupState 1} のような書き方をするものです。 トリガーを使った場合、ラベルを区別するのが困難ですから、ラベルに書けるものがいいと思います。
arw.tyx-out_mz

2019/06/21 12:46

添付ビヘイビアを使ってできました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問