WPF将椭圆渐变停止颜色绑定到值转换器



我绑定了一个椭圆复选框和一个iValueConverter (这工作…(见下文)。

 <Ellipse Name="ellLeftRoleEnabled" 
                 Fill="{Binding IsChecked, ElementName=btnRollLeftEnabled, Converter={StaticResource myColorConverter}}" 
                 Height="80" Canvas.Left="355" Stroke="#FF0C703E" Canvas.Top="440" Width="80"/>

但是现在,我如何使用这个LinearGradientBrush/GradientStop?

<Ellipse Name="ellLeftRoleMoving" Height="100" Canvas.Left="345" Stroke="Black" Canvas.Top="535" Width="100">
            <Ellipse.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="{?????} Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>

请帮助。谢谢你。

当使用它为GradientStop Color的颜色时,你不应该像第一个转换器那样返回一个Brush,而是一个Color。其余的都是一样的。

您的转换器应该返回LinearGradientBrush而不是SolidColorBrush,并保持您的xaml

public class myColorConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool) value)
            ? new LinearGradientBrush()
            {
                EndPoint = new Point(0.5, 1),
                StartPoint = new Point(0.5, 0),
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(Colors.Red, 0),
                    new GradientStop(Colors.White, 1)
                }
            }
            : new LinearGradientBrush()
            {
                EndPoint = new Point(0.5, 1),
                StartPoint = new Point(0.5, 0),
                GradientStops = new GradientStopCollection()
                {
                    new GradientStop(Colors.Blue, 0),
                    new GradientStop(Colors.Red, 1)
                }
            };
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Xaml

 <Ellipse Name="ellLeftRoleEnabled" 
             Fill="{Binding IsChecked, ElementName=btnRollLeftEnabled, Converter={StaticResource myColorConverter}}" 
             Height="80" Canvas.Left="355" Stroke="#FF0C703E" Canvas.Top="440" Width="80"/>

相关内容

  • 没有找到相关文章

最新更新