0

My goal is to change the Background (Brush) of a Control based on a certain Text property of the datasource.

I made that happen in a simpler (working) example when my converter had only 2 Properties for the brush:

<local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
                        NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>

Next step was to write that converter to handle more than 2 Strings

My converter code:

public class StringToBrushDictionary : Dictionary<string, Brush> { }

[ValueConversion(typeof(string), typeof(Brush))]
public sealed class TextToBrushConverter : IValueConverter
{

    public StringToBrushDictionary LookUpTable { get; set; }
    public Brush Default { get; set; }

    public TextToBrushConverter()
    {
        // set defaults
        LookUpTable = new StringToBrushDictionary();
        Default = Brushes.Transparent;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var collection = value as string;
        if (collection == null)
            return Default;
        if (LookUpTable.ContainsKey(collection))
            return LookUpTable[collection];
        return Default;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

My Problem now is: How do I fill that dictionary in xaml I found this 8 year old question but it isn't exactly what I need because it uses string and int, two "native" datatypes and also uses VS 2010, which has no support for dictionaries (according to the answers there). An answer there stated that a later XAML version could do this, but VS 2010 did not implement it, yet.

Here is my take on the converter Markup and what I tried already:

<Application x:Class="BetterListbox.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:BetterListbox"
              xmlns:System="clr-namespace:System;assembly=mscorlib"
             xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
             xmlns:Specialized="clr-namespace:System.Collections.Specialized;assembly=System"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis" />
        <local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
                                                        NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>

        <local:TextToBrushConverter x:Key="WarnErrorToBrush" Default="Red">
            <local:TextToBrushConverter.LookUpTable>
                <local:StringToBrushDictionary>
                    <Brush x:Key="Error" >
                        ...?                        
                    </Brush>
                    <collections:DictionaryEntry x:Key="d"  Value="Brushes.Red" />

                </local:StringToBrushDictionary>
            </local:TextToBrushConverter.LookUpTable>
        </local:TextToBrushConverter>
    </Application.Resources>
</Application>

Is it even possible to fill LookUpTable in xaml and, if yes, how?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
FrankM
  • 541
  • 3
  • 15
  • _"I found this (8 years old) Question Create a Dictionary in xaml? but this isnt excactly what I need."_ -- it sure looks like what you need. What about the answer to that question fails to address your concern? If you think it's not a duplicate, you need to actually _explain why_. Just saying it's not the answer doesn't do anyone any good. – Peter Duniho Feb 15 '18 at 09:01
  • @PeterDuniho I pointed out the differences – FrankM Feb 15 '18 at 09:07
  • No version of VS supports the new XAML features yet. You might as well be using VS2010. As far as the question of the type of the values, it is unrealistic to expect to find examples of dictionaries that match your use case exactly. Do you think that MSDN is flawed just because the documentation for `System.Collections.Generic.Dictionary` doesn't specifically mention your `Brush` scenario? ... – Peter Duniho Feb 15 '18 at 09:11
  • ... You need to distinguish in your mind between a question about dictionaries and a question about representing non-primitive types in XAML. That you need information about the latter doesn't make your question about the former novel or different from existing questions. – Peter Duniho Feb 15 '18 at 09:11
  • The question, as in the Title stated is about ```WPF Xaml markup``` I use Dictionaries every day, but i am new to WPF and xaml – FrankM Feb 15 '18 at 09:16
  • @PeterDuniho I provided as much information as is needed to reproduce my problem and help finding a quick solution, since I know your time is precious. Try to understand it from my perspective: There is that old question, you tried using to find a solution, but non of that fits. And Google didnt help much either. Should I have not open a new Question? What would be the right way to approach? – FrankM Feb 15 '18 at 09:30

1 Answers1

1

You can fill it like this:

<my:TextToBrushConverter x:Key="textToBrush">
    <my:TextToBrushConverter.LookUpTable>
        <!-- LookUpTable["red"] = Brushes.Red -->
        <x:Static MemberType="Brushes" Member="Red" x:Key="red" />
        <!-- LookUpTable["aqua"] = new SolidColorBrush(Colors.Aqua) -->
        <SolidColorBrush Color="Aqua" x:Key="aqua" />
        <!-- custom color brush -->
        <SolidColorBrush Color="#234433" x:Key="green" />
    </my:TextToBrushConverter.LookUpTable>
</my:TextToBrushConverter>
Evk
  • 98,527
  • 8
  • 141
  • 191
  • it says: "It cannot find the Member "Red" on target type" here ``````, but the other two lines work perfectly – FrankM Feb 15 '18 at 09:26
  • @FrankM that's WPF designer says that, but in reality (at runtime) it works. WPF designer is not very reliable unfortunately and may produce false errors. – Evk Feb 15 '18 at 09:26
  • You are right :) Thank you very much – FrankM Feb 15 '18 at 09:35