C# :: Aufgabe #10 :: Lösung #6

6 Lösungen Lösungen öffentlich
#10

Color ValueConverter für XAML - Ampelstatus

Fortgeschrittener - C# von pocki - 28.08.2012 um 09:59 Uhr
Schreibe einen ValueConverter für XAML (WPF, Silverlight) wobei je nach Integer-Wert (Variable Zustand) eine Ellipse eine bestimmte Farbe zugewiesen wird.

Verwende folgenden XAML-Ansatz:
Quellcode ausblenden XML-Code
<Ellipse Fill={Binding Path=Zustand, Converter={StaticResource ResourceKey=StateToColorConverter}, ConverterParameter=Red;Orange;Green}/>


Wobei die Zuordnung wiefolgt sein soll:
Quellcode ausblenden C#-Code
Zustand=1; //Red
Zustand=2; //Orange
Zustand=3; //Green
//Für alle anderen Zustandswerte: Transparent
#6
vote_ok
von RevTreb (860 Punkte) - 27.11.2018 um 15:00 Uhr
Quellcode ausblenden XML-Code
<Window x:Class="ColoredEllipse.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:ColoredEllipse"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:StateToColorConverter x:Key="StateToColorConverter"/>
        <local:MainWindowViewModel x:Key="myViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource myViewModel}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="200"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Width="{Binding Parent.ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}">
            <Ellipse Fill="{Binding Path=Zustand, Converter={StaticResource ResourceKey=StateToColorConverter}, ConverterParameter=Red;Orange;Green}"/>
        </Grid>
        <StackPanel Grid.Column="1" Margin="10">
            <TextBox Height="30" Text="{Binding Zustand, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBox Height="30" Text="{Binding Zustand, ConverterParameter=Red;Orange;Green, Converter={StaticResource StateToColorConverter}}"></TextBox>
        </StackPanel>
    </Grid>
</Window>


Quellcode ausblenden C#-Code
using System.ComponentModel;

namespace ColoredEllipse
{
    class MainWindowViewModel:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private int zustand;

        public int Zustand
        {
            get { return zustand; }
            set
            {
                if (zustand!=value)
                {
                    zustand = value;
                    if (PropertyChanged!=null)
                    {
                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Zustand"));
                    }
                }
                
            }
        }

        public MainWindowViewModel()
        {
            Zustand = 1;
        }
    }
}


Quellcode ausblenden C#-Code
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace ColoredEllipse
{
    public class StateToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string p = parameter as string;
            string[] colors = p.Split(';');
            int actValue = (int)value - 1;
            object retval = null;

            if (targetType==typeof(string))
            {
                string temp = "Transparent";
                if (actValue >= 0 && actValue < 3)
                {
                    temp = colors[actValue];
                }
                retval = temp;
            }
            if (targetType == typeof(Brush))
            {
                SolidColorBrush myBrush;
                string colorString= "Transparent";
                if (actValue >= 0 && actValue < 3)
                {
                    colorString = colors[actValue];
                }

                Color myColor = (Color)ColorConverter.ConvertFromString(colorString);
                myBrush = new SolidColorBrush(myColor);

                retval = myBrush;
            }

            return retval;
        }

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

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben