C# :: Aufgabe #10
6 Lösungen

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:
XML-Code
Wobei die Zuordnung wiefolgt sein soll:
C#-Code
Verwende folgenden XAML-Ansatz:

<Ellipse Fill={Binding Path=Zustand, Converter={StaticResource ResourceKey=StateToColorConverter}, ConverterParameter=Red;Orange;Green}/>
Wobei die Zuordnung wiefolgt sein soll:

Zustand=1; //Red Zustand=2; //Orange Zustand=3; //Green //Für alle anderen Zustandswerte: Transparent
Lösungen:
Hier ist mein Code dafür:
C#-Code

public class StateToColorConverter : System.Windows.Data.IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null || parameter == null || !(value is int)) { return System.Windows.Media.Brushes.Transparent; } string[] parColors = parameter.ToString().Split(';'); if (parColors.Length == 3) { System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter(); int Zustand = (int)value; switch (Zustand) { case 1: //Da Parameter in diesem Fall ein string ist, muss dieser zuvor als zum Typ Brush konvertiert werden. return bc.ConvertFromString(parColors[0]) as System.Windows.Media.SolidColorBrush; case 2: return bc.ConvertFromString(parColors[1]) as System.Windows.Media.SolidColorBrush; case 3: return bc.ConvertFromString(parColors[2]) as System.Windows.Media.SolidColorBrush; default: break; } } return System.Windows.Media.Brushes.Transparent; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return new NotImplementedException(); } }
ich bin nicht ganz zufrieden mit der lösung
XML-Code
C#-Code

<Window.Resources> <l:StateToColorConverter x:Key="StateToColorConverter"/> </Window.Resources> <Grid> <Ellipse Height="100" Fill="{Binding Path=Zustand, Converter={StaticResource ResourceKey=StateToColorConverter }, ConverterParameter=Red;Orange;Green}"/> </Grid>

public class StateToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string[] colors = (parameter as string).Split(';'); int state = value != null ? (int) value : colors.Length; if (state == 0) return "Transparent"; return colors[state-1]; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } }

<Window x:Class="Ampelstatus.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Ampelstatus" Title="MainWindow" Height="350" Width="250"> <Window.Resources> <local:AmpelstatusConverter x:Key="StateToColorConverter" /> </Window.Resources> <Grid> <StackPanel> <TextBox Height="25" TextAlignment="Center" Name="ZustandTextBox" /> </StackPanel> <Ellipse Height="250" Margin="0,20,0,0" Fill="{Binding Path=Text,ElementName=ZustandTextBox, Converter={StaticResource ResourceKey=StateToColorConverter}, ConverterParameter=Red;Orange;Green}"/> </Grid> </Window>

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using System.Windows.Media; namespace Ampelstatus { public class AmpelstatusConverter : IValueConverter { object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var p = parameter as string; string[] farben = p.Split(';'); SolidColorBrush solidColorBrush = new SolidColorBrush(); switch(value as string){ case "1": solidColorBrush.Color = (Color)ColorConverter.ConvertFromString(farben[0]); break; case "2": solidColorBrush.Color = (Color)ColorConverter.ConvertFromString(farben[1]); break; case "3": solidColorBrush.Color = (Color)ColorConverter.ConvertFromString(farben[2]); break; } return solidColorBrush; } object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } } }

<Window x:Class="Exercise_10.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:Exercise_10" mc:Ignorable="d" Title="MainWindow" Height="350" Width="329.045"> <Window.Resources> <local:AmpelstatusConverter x:Key="StateToColorConverter" /> </Window.Resources> <Grid> <StackPanel> <TextBox Height="25" TextAlignment="Center" Name="StatusInputBox" /> </StackPanel> <Ellipse Height="250" Margin="0,44,0,25" Fill="{Binding Path=Text, ElementName=StatusInputBox, Converter={StaticResource ResourceKey=StateToColorConverter}, ConverterParameter=Red;Orange;Green}" /> </Grid> </Window>

namespace Exercise_10 { using System; using System.Globalization; using System.Windows.Data; using System.Windows.Media; using static System.Windows.Media.ColorConverter; public class AmpelstatusConverter : IValueConverter { object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) { var p = parameter as string; string[] farben = p.Split(';'); var solidColorBrush = new SolidColorBrush(); switch (value as string) { case "1": solidColorBrush.Color = (Color) ConvertFromString(farben[0]); break; case "2": solidColorBrush.Color = (Color) ConvertFromString(farben[1]); break; case "3": solidColorBrush.Color = (Color) ConvertFromString(farben[2]); break; default: solidColorBrush.Color = (Color) ConvertFromString("White"); break; } return solidColorBrush; } object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } } }

using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace TrainYourProgrammer10 { /// <summary> /// Interaktionslogik für MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); StackPanel myStackPanel = new StackPanel(); Ellipse myEllipse = new Ellipse(); SolidColorBrush mySolidColorBrush = new SolidColorBrush(); myStackPanel.Children.Add(myEllipse); this.Content = myStackPanel; int Zustand = 0; switch (Zustand) { case 1: mySolidColorBrush.Color = Color.FromArgb(255, 255, 0, 0); break; case 2: mySolidColorBrush.Color = Color.FromArgb(255, 255, 69, 0); break; case 3: mySolidColorBrush.Color = Color.FromArgb(255, 0, 255, 0); break; default: mySolidColorBrush.Color = Color.FromArgb(0, 0, 0, 0); break; } myEllipse.Fill = mySolidColorBrush; myEllipse.StrokeThickness = 2; myEllipse.Stroke = Brushes.Black; myEllipse.Width = 200; myEllipse.Height = 100; this.Content = myStackPanel; } } }

<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>

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; } } }

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; } } }