WPF绑定改变椭圆的填充颜色

时间:2021-11-08 09:14:18

Probably a simple question but:

可能是一个简单的问题,但是

How do I programmatically change the color of an ellipse that is defined in XAML based on a variable?

如何以编程方式更改基于变量在XAML中定义的椭圆的颜色?

Everything I've read on binding is based on collections and lists -can't I set it simply (and literally) based on the value of a string variable? string color = "red" color = "#FF0000"

我在绑定上读到的所有内容都是基于集合和列表的——难道我不能简单地(从字面上)基于字符串变量的值来设置它吗?string color = "red" color = "#FF0000"

3 个解决方案

#1


16  

It's worth pointing out that the converter the other posts reference already exists, which is why you can do <Ellipse Fill="red"> in xaml in the first place. The converter is System.Windows.Media.BrushConverter:

值得指出的是,其他post引用的转换器已经存在,这就是为什么您可以首先在xaml中执行 。转换器System.Windows.Media.BrushConverter:

        BrushConverter bc = new BrushConverter();
        Brush brush = (Brush) bc.ConvertFrom("Red");

The more efficient way is to use the full syntax:

更有效的方法是使用完整的语法:

myEllipse.Fill = new SolidColorBrush(Colors.Red);

EDIT in response to -1 and comments:

编辑回应-1和评论:

The code above works perfectly fine in code, which is what the original question was asking about. You also don't want an IValueConverter - these are typically used for binding scenarios. A TypeConverter is the right solution here (because you're one-way converting a string to a brush). See this article for details.

上面的代码在代码中工作得非常好,这正是最初的问题所在。您也不需要IValueConverter——这些通常用于绑定场景。TypeConverter是正确的解决方案(因为您是单向的将字符串转换为画笔)。有关细节,请参阅本文。

Further edit (having reread Aviad's comment): you don't need to explicitly use the TypeConverter in Xaml - it's used for you. If I write this in Xaml:

进一步编辑(重读Aviad的评论):您不需要在Xaml中显式地使用TypeConverter——它是为您使用的。如果我用Xaml写:

<Ellipse Fill="red">

... then the runtime automagically uses a BrushConverter to turn the string literal into a brush. That Xaml is essentially converted into the equivalent longhand:

…然后运行时自动使用笔刷转换器将字符串文字转换为笔刷。Xaml本质上被转化为等价的longhand:

<Ellipse>
  <Ellipse.Fill>
     <SolidColorBrush Color="#FFFF0000" />
  </Ellipse.Fill>             
</Ellipse>

So you're right - you can't use it in Xaml - but you don't need to.

你是对的,你不能在Xaml中使用它,但你不需要。

Even if you had a string value that you wanted to bind in as the fill, you don't need to specify the converter manually. This test from Kaxaml:

即使您想要将一个字符串值绑定到填充,也不需要手动指定转换器。Kaxaml这个测试:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:s="clr-namespace:System;assembly=mscorlib">
  <Page.Resources>
    <s:String x:Key="col">Red</s:String>
  </Page.Resources>

  <StackPanel>  
    <Ellipse Width="20" Height="20" Fill="{Binding Source={StaticResource col}}" />
  </StackPanel>
</Page>

Strangely, you can't just use the StaticResource col and still have this work - but with the binding it and automatically uses the ValueConverter to turn the string into a brush.

奇怪的是,您不能仅仅使用StaticResource col,而且仍然有这个工作——但是绑定它并自动使用ValueConverter将字符串转换成画笔。

#2


6  

what you will need to do is implement a custom converter to convert the colour to the brush object. Something like this...

您需要做的是实现一个自定义转换器来将颜色转换为画笔对象。是这样的……

public class ColorToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Drawing.Color col = (System.Drawing.Color)value;
        Color c = Color.FromArgb(col.A, col.R, col.G, col.B);
        return new System.Windows.Media.SolidColorBrush(c);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        SolidColorBrush c = (SolidColorBrush)value;
        System.Drawing.Color col = System.Drawing.Color.FromArgb(c.Color.A, c.Color.R, c.Color.G, c.Color.B);
        return col;
    }
}

And then specify that converter in your binding

然后在绑定中指定该转换器

Fill="{Binding Colors.Red, Converter={StaticResource ColorToBrushConverter }"

#3


1  

use

使用

"System.Windows.Media"

if the 'Name' of of your ellipse in your xaml is "my_ellipse", in your code something like this:

如果xaml中椭圆的“名称”是“my_ellipse”,那么在代码中应该是这样的:

my_ellipse.Fill = System.Windows.Media.Brushes.Red;

or this:

或:

my_ellipse.Fill = (SolidColorBrush)new BrushConverter().ConvertFromString("#F4F4F5")

#1


16  

It's worth pointing out that the converter the other posts reference already exists, which is why you can do <Ellipse Fill="red"> in xaml in the first place. The converter is System.Windows.Media.BrushConverter:

值得指出的是,其他post引用的转换器已经存在,这就是为什么您可以首先在xaml中执行 。转换器System.Windows.Media.BrushConverter:

        BrushConverter bc = new BrushConverter();
        Brush brush = (Brush) bc.ConvertFrom("Red");

The more efficient way is to use the full syntax:

更有效的方法是使用完整的语法:

myEllipse.Fill = new SolidColorBrush(Colors.Red);

EDIT in response to -1 and comments:

编辑回应-1和评论:

The code above works perfectly fine in code, which is what the original question was asking about. You also don't want an IValueConverter - these are typically used for binding scenarios. A TypeConverter is the right solution here (because you're one-way converting a string to a brush). See this article for details.

上面的代码在代码中工作得非常好,这正是最初的问题所在。您也不需要IValueConverter——这些通常用于绑定场景。TypeConverter是正确的解决方案(因为您是单向的将字符串转换为画笔)。有关细节,请参阅本文。

Further edit (having reread Aviad's comment): you don't need to explicitly use the TypeConverter in Xaml - it's used for you. If I write this in Xaml:

进一步编辑(重读Aviad的评论):您不需要在Xaml中显式地使用TypeConverter——它是为您使用的。如果我用Xaml写:

<Ellipse Fill="red">

... then the runtime automagically uses a BrushConverter to turn the string literal into a brush. That Xaml is essentially converted into the equivalent longhand:

…然后运行时自动使用笔刷转换器将字符串文字转换为笔刷。Xaml本质上被转化为等价的longhand:

<Ellipse>
  <Ellipse.Fill>
     <SolidColorBrush Color="#FFFF0000" />
  </Ellipse.Fill>             
</Ellipse>

So you're right - you can't use it in Xaml - but you don't need to.

你是对的,你不能在Xaml中使用它,但你不需要。

Even if you had a string value that you wanted to bind in as the fill, you don't need to specify the converter manually. This test from Kaxaml:

即使您想要将一个字符串值绑定到填充,也不需要手动指定转换器。Kaxaml这个测试:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:s="clr-namespace:System;assembly=mscorlib">
  <Page.Resources>
    <s:String x:Key="col">Red</s:String>
  </Page.Resources>

  <StackPanel>  
    <Ellipse Width="20" Height="20" Fill="{Binding Source={StaticResource col}}" />
  </StackPanel>
</Page>

Strangely, you can't just use the StaticResource col and still have this work - but with the binding it and automatically uses the ValueConverter to turn the string into a brush.

奇怪的是,您不能仅仅使用StaticResource col,而且仍然有这个工作——但是绑定它并自动使用ValueConverter将字符串转换成画笔。

#2


6  

what you will need to do is implement a custom converter to convert the colour to the brush object. Something like this...

您需要做的是实现一个自定义转换器来将颜色转换为画笔对象。是这样的……

public class ColorToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Drawing.Color col = (System.Drawing.Color)value;
        Color c = Color.FromArgb(col.A, col.R, col.G, col.B);
        return new System.Windows.Media.SolidColorBrush(c);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        SolidColorBrush c = (SolidColorBrush)value;
        System.Drawing.Color col = System.Drawing.Color.FromArgb(c.Color.A, c.Color.R, c.Color.G, c.Color.B);
        return col;
    }
}

And then specify that converter in your binding

然后在绑定中指定该转换器

Fill="{Binding Colors.Red, Converter={StaticResource ColorToBrushConverter }"

#3


1  

use

使用

"System.Windows.Media"

if the 'Name' of of your ellipse in your xaml is "my_ellipse", in your code something like this:

如果xaml中椭圆的“名称”是“my_ellipse”,那么在代码中应该是这样的:

my_ellipse.Fill = System.Windows.Media.Brushes.Red;

or this:

或:

my_ellipse.Fill = (SolidColorBrush)new BrushConverter().ConvertFromString("#F4F4F5")