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"

相关推荐
暴走约伯11 小时前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316341 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船2 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261352 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船2 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
huaqianzkh4 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
用户3721574261354 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
公子小六5 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技5 天前
第6章第4节:自定义分词器
开发语言·c#
zhanghaha13145 天前
C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
开发语言·数据库·c#