根据从绑定接收到的数据,在XAML中更改颜色StackPanel

根据从绑定接收到的数据,在XAML中更改颜色StackPanel,xaml,windows-phone-8,Xaml,Windows Phone 8,我想改变StackPanel的颜色,根据数据1,2,3。。。从绑定值接收,我应该使用哪个 <StackPanel Grid.Column="1"> <Border Height="50" Width="Auto" Margin="3" Background="Black"> <TextBlock FontSize="18" FontWeight="Bold" TextWrapping="Wrap" Foreground="White"

我想改变StackPanel的颜色,根据数据1,2,3。。。从绑定值接收,我应该使用哪个

<StackPanel Grid.Column="1">
   <Border Height="50" Width="Auto" Margin="3" Background="Black">
      <TextBlock FontSize="18" FontWeight="Bold" TextWrapping="Wrap" Foreground="White" 
                 Text="{Binding valor}" VerticalAlignment="Center"
                 HorizontalAlignment="Center"/>
   </Border>
</StackPanel>
使用转换器:

使用转换器:


我通过在类中创建一个新变量并在xaml后台={Binding customBackground}中调用这个新变量来修复它

public object customBackground
    {
    get
    {
    if (color == "1")//GREEN 
        {
            return new SolidColorBrush(Color.FromArgb(255, 7, 166, 45));
        }
    if (color == "2")//RED
        {
            return new SolidColorBrush(Color.FromArgb(255, 230, 5, 10));
        }
    if (color == "3")//BLUE
    {
        return new SolidColorBrush(Color.FromArgb(255, 6, 102, 210));
    }
    if (color == "4")//YELLOW
    {
        return new SolidColorBrush(Color.FromArgb(255, 189, 211, 3));
    }
    // Color por defecto
    return new SolidColorBrush(Colors.Gray);
    }
    }

我通过在类中创建一个新变量并在xaml后台={Binding customBackground}中调用这个新变量来修复它

public object customBackground
    {
    get
    {
    if (color == "1")//GREEN 
        {
            return new SolidColorBrush(Color.FromArgb(255, 7, 166, 45));
        }
    if (color == "2")//RED
        {
            return new SolidColorBrush(Color.FromArgb(255, 230, 5, 10));
        }
    if (color == "3")//BLUE
    {
        return new SolidColorBrush(Color.FromArgb(255, 6, 102, 210));
    }
    if (color == "4")//YELLOW
    {
        return new SolidColorBrush(Color.FromArgb(255, 189, 211, 3));
    }
    // Color por defecto
    return new SolidColorBrush(Colors.Gray);
    }
    }

您好,我试图在windows phone 8中使用此示例,但使用标记时出错,这用于wp8应用?对于wp8应用,应使用:代替您好,我尝试在windows phone 8中使用此示例,但使用标记时出错,这用于wp8应用?对于wp8应用,应使用:代替
public class ColorConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,CultureInfo culture)
    {
        if (value != null)
        {
            int Value = (int)value;

            if(Value == 1)
               return Colors.Green;
             else if(Value == 2)
               return Colors.Red;
[...]
            }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
public object customBackground
    {
    get
    {
    if (color == "1")//GREEN 
        {
            return new SolidColorBrush(Color.FromArgb(255, 7, 166, 45));
        }
    if (color == "2")//RED
        {
            return new SolidColorBrush(Color.FromArgb(255, 230, 5, 10));
        }
    if (color == "3")//BLUE
    {
        return new SolidColorBrush(Color.FromArgb(255, 6, 102, 210));
    }
    if (color == "4")//YELLOW
    {
        return new SolidColorBrush(Color.FromArgb(255, 189, 211, 3));
    }
    // Color por defecto
    return new SolidColorBrush(Colors.Gray);
    }
    }