2016-09-14 2 views
0

私はタイプSolidColorBrushのオブジェクトを持っていて、それは SolidColorBrushを保持しています。私のコンバータークラスへのC#SolidColorBrush

私はリストにバインドされた私のdataGrid用のコンバータを持っています。 このDataGridの各行は、私が持っているConverterによって色付けされます。

すべて正常に動作しますが、静的な「Brushes.Red」ではなく、どのようにしてSolidColorBrushオブジェクトを返すことができますか。

マイコンバータ:

[ValueConversion(typeof(MainWindow.eErrorLevel), typeof(Brush))] 
public class TypeToColourConverter : IValueConverter 
{ 
    #region IValueConverter Members 
    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     MainWindow.eErrorLevel errorLevel = (MainWindow.eErrorLevel)value; 



     switch (errorLevel) 
     { 
      case MainWindow.eErrorLevel.Information: 
       return Brushes.Red; 


      case MainWindow.eErrorLevel.Warning: 
       return Brushes.Yellow; 

      case MainWindow.eErrorLevel.Error: 
       return Brushes.Red; 

     } 

     return Brushes.Gray; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

    #endregion 
} 

thatsの重要 そして公開されている私のメインウィンドウで[マイSolidColorBrushのオブジェクトならば私のコンバータは、メインウィンドウではありません。

public CurrentColor CurrentColors = new CurrentColor(); 



    public class CurrentColor 
    { 
     public SolidColorBrush ERROR { get; set; } 
     public SolidColorBrush WARNING { get; set; } 
     public SolidColorBrush INFORMATION { get; set; } 
    } 

EDIT:私のブラシをすることができユーザー自身によって動的に設定されます

EDIT2:今すぐ働いていただきありがとうございます:)

+0

あなたは変換パラメータとして 'CurrentColor' _(StaticResource)_のインスタンスを渡すことができます。 –

+0

それはどうやって説明できますか? –

+0

3つのSolidColorBrushプロパティをTypeToColourConverterクラスに配置し、コンバータリソースを宣言するときにその値を設定することもできます。 – Clemens

答えて

0

私は私のコメントで言ったように、ここにconverterparameterとしてそれを渡して、例ですが、選択肢はおそらくあります

XAML

<Window x:Class="WpfApplicationTestColorConverter.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplicationTestColorConverter" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:ErrorColors x:Key="Colors" /> 
     <local:TypeToColourConverter x:Key="ColorConverter" /> 
    </Window.Resources> 
    <Grid> 
     <ListBox x:Name="ListBox1" ItemsSource="{Binding MyObjects}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <TextBlock 
         Text="{Binding Title}" 
         Background="{Binding ErrorLevel, 
          Converter={StaticResource ColorConverter}, 
          ConverterParameter={StaticResource Colors}}" /> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Window> 

コードが

public partial class MainWindow : Window 
{ 
    public ObservableCollection<MyObject> MyObjects { get; } = new ObservableCollection<MyObject>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 

     // find the (static)resource 
     var colors = (ErrorColors)FindResource("Colors"); 
     colors.ERROR = new SolidColorBrush(Colors.Red); 
     colors.WARNING = new SolidColorBrush(Colors.Orange); 
     colors.INFORMATION = new SolidColorBrush(Colors.Lime); 

     // Add objects to the list 
     MyObjects.Add(new MyObject { Title = "This is an error", ErrorLevel = ErrorLevel.Error }); 
     MyObjects.Add(new MyObject { Title = "This is a warning", ErrorLevel = ErrorLevel.Warning }); 
     MyObjects.Add(new MyObject { Title = "This is information", ErrorLevel = ErrorLevel.Information }); 
    } 
} 
をbehide

コンバータ

[ValueConversion(typeof(ErrorLevel), typeof(Brush))] 
public class TypeToColourConverter : IValueConverter 
{ 
    #region IValueConverter Members 
    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     if (!(value is ErrorLevel)) 
      return Brushes.Gray; 

     if (!(parameter is ErrorColors)) 
      return Brushes.Gray; 

     var lvl = (ErrorLevel)value; 
     var currentColor = (ErrorColors)parameter; 

     switch (lvl) 
     { 
      case ErrorLevel.Information: 
       return currentColor.INFORMATION; 


      case ErrorLevel.Warning: 
       return currentColor.WARNING; 

      case ErrorLevel.Error: 
       return currentColor.ERROR; 

     } 

     return Brushes.Gray; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

    #endregion 
} 

public class ErrorColors 
{ 
    public SolidColorBrush ERROR { get; set; } 
    public SolidColorBrush WARNING { get; set; } 
    public SolidColorBrush INFORMATION { get; set; } 
} 

public enum ErrorLevel 
{ 
    Error, 
    Warning, 
    Information 
} 

public class MyObject 
{ 
    public string Title { get; set; } 
    public ErrorLevel ErrorLevel { get; set; } 
} 

結果:

ResultScreen

+0

助けてくれてありがとう、私はあなたがやっていることのポイントを得たと思う...しかし、私の新しい問題は私です私のコンバータのトラフに戻ってブラシを返すそれは私に例外を与える:ObjectindexはOBject instaceを指摘していなかった...そのようなもの –

+0

オオカハオ、オハイオ州、今私はそれを得た:)私の問題は、私が間違った私はそれが欲しいと思っていました。とても感謝しています。3 –

1

これらの色は、実行時に変更されないこと、あなたのコンバータ上のリソースとしてあなたのブラシを宣言し、次のように各ブラシのためにあなたのコンバータにプロパティを追加したと仮定すると:

[ValueConversion(typeof(MainWindow.eErrorLevel), typeof(Brush))] 
public class TypeToColourConverter : IValueConverter 
{ 
    #region IValueConverter Members 
    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     MainWindow.eErrorLevel errorLevel = (MainWindow.eErrorLevel)value; 

     switch (errorLevel) 
     { 
      case MainWindow.eErrorLevel.Information: 
       return Error; 


      case MainWindow.eErrorLevel.Warning: 
       return Warning; 

      case MainWindow.eErrorLevel.Error: 
       return Information; 

     } 

     return Normal; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

    #endregion 

    public Brush Normal { get; set; } 

    public Brush Error { get; set; } 

    public Brush Warning { get; set; } 

    public Brush Information { get; set; } 
} 

にあなたのコンバータを改正

(あなたのコンバータが追加されている場所)あなたのXAMLを改正:

<SolidColorBrush x:Key="Normal" Color="#FFAAAAAA"/> 
<SolidColorBrush x:Key="Error" Color="#FFFF0000"/> 
<SolidColorBrush x:Key="Warning" Color="#FF00FF00"/> 
<SolidColorBrush x:Key="Information" Color="#FF0000FF"/> 

<local:TypeToColourConverter x:Key="TypeToColourConverter" Normal="{StaticResource Normal}" Error="{StaticResource Error}" Warning="{StaticResource Warning}" Information="{StaticResource Information}" /> 

これは非常に「デザイナーフレンドリー」(すなわちですBlendでこれらの色をすべて変更でき、維持しやすくなります。

希望します。

+0

私は私の色が動的にユーザーによって動的に変わることができると言いました^^申し訳ありません:c –

関連する問題