简体   繁体   English

绑定到XAML中对象的属性

[英]Binding to a property of the object in XAML

I have the following data template: 我有以下数据模板:

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

This is being populated with Pokemon objects: 这是用Pokemon对象填充的:

public class Pokemon()
{
    public PokeType Type1 { get; }
    public PokeType Type2 { get; }
    public string Name { get; }
}

which, in turn, contain PokeType objects: 依次包含PokeType对象:

public class PokeType()
{
    public string TypeColor { get; } // "#FF0000", can be changed if that'd make things easier
    public string Name { get; }
}

Converter 转换器

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

How can I achieve the desired effect (binding the type's name as the text and coloring it with the type's color) in the best manner? 如何以最佳方式达到预期的效果(将类型的名称绑定为文本并用类型的颜色为其着色)? Thanks! 谢谢!

I'd recommend using an IValueConverter - have the Foreground bindings be Type1.TypeColor and Type2.TypeColor, but add the Converter parameter. 我建议使用IValueConverterForeground绑定设置为Type1.TypeColor和Type2.TypeColor,但是添加Converter参数。

You would also have to create a String to Color converter (you can find an example here: http://compiledexperience.com/blog/posts/useful-calue-converters/ ) 您还必须创建一个字符串到颜色转换器(您可以在此处找到示例: http : //compiledexperience.com/blog/posts/useful-calue-converters/

Usage would be something like: 用法将类似于:

<Page.Resources>
<ColorToBrushConverter x:Key="ColorToBrush"/>
</Page.Resources>

...

<TextBlock Text="{Binding Type1.Name}" Foreground="{Binding Type1.Color, Converter={StaticResource ColourToBrushConverter}}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM