WPF多值转换器

背景:实现Slider拖动可以调整rgb

单转换器:WPF中数据绑定转换器Converter-CSDN博客

在View中:

XML 复制代码
<StackPanel Orientation="Vertical">
    <Slider x:Name="slider_R" Minimum="0" Maximum="255" Width="200" Margin="20"/>
    <Slider x:Name="slider_G" Minimum="0" Maximum="255" Width="200" Margin="20"/>
    <Slider x:Name="slider_B" Minimum="0" Maximum="255" Width="200" Margin="20"/>

    <Path VerticalAlignment="Center" HorizontalAlignment="Center">
        <Path.Data>
            <EllipseGeometry Center="50, 50" RadiusX="50" RadiusY="50"/>
        </Path.Data>

        <Path.Fill>
            <MultiBinding Converter="{StaticResource rmc}">
                <Binding ElementName="slider_R" Path="Value"/>
                <Binding ElementName="slider_G" Path="Value"/>
                <Binding ElementName="slider_B" Path="Value"/>
            </MultiBinding>
        </Path.Fill>
    </Path>
</StackPanel>

-- Fill中绑定的资源是Window.Resources中引入进来的转换器key

-- 转换器返回的是一个SolidBrush,其实就是相当于在这个地方写个SolidBrush

转换器中:

cs 复制代码
public class rgbMultiConventer : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values==null || values.Length < 2)
        {
            return null;
        }

        byte r = System.Convert.ToByte(values[0]);
        byte g = System.Convert.ToByte(values[1]);
        byte b = System.Convert.ToByte(values[2]);

        Color color = Color.FromRgb(r, g, b);

        return new SolidColorBrush(color);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

-- 就是转换器类接上接口"IMultiValueConverter"

相关推荐
kaikaile19957 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
wearegogog12312 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
糖不吃12 小时前
WPF值转换器
c#
Popeye-lxw14 小时前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL162386312914 小时前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
故渊at14 小时前
第二板块:Android 四大组件标准化学理 | 第十二篇:四大组件全景总结与系统服务(System Server)架构
android·架构·wpf·四大组件·system service
aini_lovee16 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路16 小时前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白11116 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays101117 小时前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel