Laden...

[gelöst] IntToBrushConvrter mit Int,Brush Diconary, ist das möglich?

Erstellt von jogibear9988 vor 13 Jahren Letzter Beitrag vor 13 Jahren 1.388 Views
J
jogibear9988 Themenstarter:in
641 Beiträge seit 2007
vor 13 Jahren
[gelöst] IntToBrushConvrter mit Int,Brush Diconary, ist das möglich?

Ich hätte gernen folgenden Conveter:


public class IntToBrushConverter : IValueConverter
    {
        public Dictionary<int, Brush> IntBrushList { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !IntBrushList.ContainsKey((int)value))
                return null;
            else
                return IntBrushList[(int) value];
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Jetzt ist nur die Frage, kann Ich die eigenschaft IntBrushList in XAML setzen?


<ResourceDictionary> 
<ExampleWPFVisualization:IntToBrushConverter IntBrushList=""></ExampleWPFVisualization:IntToBrushConverter>
</ResourceDictionary> 

Was müsste Ich nun bei IntBrushList hinschreiben? (Ich glaub ja es geht nicht!)

Oder hat jemand eine gute Idee wie Ich das sonst lösen kann? Bekomme versch. Int Werte und muss diesen Farben zuordnen, und möchte das alles gerne ohne Codebehind machen!

cSharp Projekte : https://github.com/jogibear9988

3.430 Beiträge seit 2007
vor 13 Jahren

Hallo jogibear9988,

Oder hat jemand eine gute Idee wie Ich das sonst lösen kann?

Du könntest es mal mit einen DataTrigger versuchen.
Das ist wohl die einfachste Lösung.

Hier siehst du ein Beispiel: WPF Styles and Control Templates

Gruß
Michael

J
jogibear9988 Themenstarter:in
641 Beiträge seit 2007
vor 13 Jahren

Wäre ne Idee.

Oder würde sowas in der Richtung gehen:

Converter:


    public class IntRangeBrushValue
    {
        public string IntRange { get; set; }
        public Brush Brush { get; set; }
    }


    public class IntRangeToBrushConverter : IValueConverter
    {
        public IntRangeBrushValue[] IntRangeBrushValues { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;

            int intvalue = (int) value;
            foreach (IntRangeBrushValue intBrushValue in IntRangeBrushValues)
            {
                if (intBrushValue.IntRange.Contains('-'))
                {
                    int start = System.Convert.ToInt32(intBrushValue.IntRange.Split('-')[0]);
                    int stop = System.Convert.ToInt32(intBrushValue.IntRange.Split('-')[1]);
                    if (intvalue >= start && intvalue <= stop)
                        return intBrushValue.Brush;
                }
                else if (System.Convert.ToInt32(intBrushValue.IntRange) == intvalue)
                    return intBrushValue.Brush;
            }

            return null;          
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

XAML:


 <ResourceDictionary>
            <ExampleWPFVisualization:IntRangeToBrushConverter x:Key="FarbenConverter1">
                <ExampleWPFVisualization:IntRangeToBrushConverter.IntRangeBrushValues>
                    <x:Array Type="ExampleWPFVisualization:IntRangeBrushValue">
                        <ExampleWPFVisualization:IntRangeBrushValue IntRange="0-99" Brush="Red" />
                        <ExampleWPFVisualization:IntRangeBrushValue IntRange="100" Brush="Blue" />
                        <ExampleWPFVisualization:IntRangeBrushValue IntRange="101-200" Brush="Red" />
                    </x:Array>
                </ExampleWPFVisualization:IntRangeToBrushConverter.IntRangeBrushValues>
            </ExampleWPFVisualization:IntRangeToBrushConverter>
 </ResourceDictionary>

funzt noch net, Fehler: "A value of type 'ArrayExtension' cannot be added to a collection or dictionary of type 'IntRangeBrushValue[]'".

Kann Ich das irgendwie machen?

cSharp Projekte : https://github.com/jogibear9988

J
jogibear9988 Themenstarter:in
641 Beiträge seit 2007
vor 13 Jahren

So scheints zu gehen:


public class IntRangeBrushValue
    {
        public string IntRange { get; set; }
        public Brush Brush { get; set; }
    }


    public class IntRangeToBrushConverter : IValueConverter
    {
        private List<IntRangeBrushValue> _intRangeBrushValues=new List<IntRangeBrushValue>();
        public List<IntRangeBrushValue> IntRangeBrushValues
        {
            get { return _intRangeBrushValues; }
            set { _intRangeBrushValues = value; }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;

            int intvalue = (int) value;
            foreach (IntRangeBrushValue intBrushValue in IntRangeBrushValues)
            {
                if (intBrushValue.IntRange.Contains('-'))
                {
                    int start = System.Convert.ToInt32(intBrushValue.IntRange.Split('-')[0]);
                    int stop = System.Convert.ToInt32(intBrushValue.IntRange.Split('-')[1]);
                    if (intvalue >= start && intvalue <= stop)
                        return intBrushValue.Brush;
                }
                else if (System.Convert.ToInt32(intBrushValue.IntRange) == intvalue)
                    return intBrushValue.Brush;
            }

            return null;          
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


<ResourceDictionary>
            <ExampleWPFVisualization:IntRangeToBrushConverter x:Key="FarbenConverter1">
                <ExampleWPFVisualization:IntRangeToBrushConverter.IntRangeBrushValues>                    
                        <ExampleWPFVisualization:IntRangeBrushValue IntRange="0-99" Brush="Red" />
                        <ExampleWPFVisualization:IntRangeBrushValue IntRange="100" Brush="Blue" />
                        <ExampleWPFVisualization:IntRangeBrushValue IntRange="101-200" Brush="Red" />                    
                </ExampleWPFVisualization:IntRangeToBrushConverter.IntRangeBrushValues>
            </ExampleWPFVisualization:IntRangeToBrushConverter>

cSharp Projekte : https://github.com/jogibear9988