У меня есть следующий шаблон данных:Привязка к свойству объекта в XAML
<Page.Resources>
<local:ColorToBrushConverter x:Key="ColorToBrush"/>
<DataTemplate x:Key="PokedexListTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0"/>
<StackPanel Grid.Column="1">
<TextBlock Text="Type1" Foreground="{Binding Type1.Color, Converter={StaticResource ColorToBrush}}"/>
<TextBlock Text="Type2" Foreground="{Binding Type2.Color, Converter={StaticResource ColorToBrush}}"/>
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
Это время заполняется Pokemon
объектов:
public class Pokemon()
{
public PokeType Type1 { get; }
public PokeType Type2 { get; }
public string Name { get; }
}
, который, в свою очередь, содержит PokeType
объектов:
public class PokeType()
{
public string TypeColor { get; } // "#FF0000", can be changed if that'd make things easier
public string Name { get; }
}
конвертер
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string culture)
{
return new SolidColorBrush(Colors.Red);
}
public object ConvertBack(object value, Type targetType, object parameter, string culture)
{
throw new NotSupportedException();
}
private static Color Parse(string color)
{
var offset = color.StartsWith("#") ? 1 : 0;
var a = Byte.Parse(color.Substring(0 + offset, 2), NumberStyles.HexNumber);
var r = Byte.Parse(color.Substring(2 + offset, 2), NumberStyles.HexNumber);
var g = Byte.Parse(color.Substring(4 + offset, 2), NumberStyles.HexNumber);
var b = Byte.Parse(color.Substring(6 + offset, 2), NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
}
Как можно достичь желаемого эффекта (связывающую имя типа как текст и окрашивая его с цветом типа по) наилучшим образом? Благодаря!