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"

相关推荐
小羊没烦恼!12 小时前
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
java·开发语言·前端·数据库·算法·c#
UIU11415 小时前
头文件可以防什么错?运用头文件做声明
c++·学习·c#
一条闲鱼_mytube20 小时前
高并发短链接系统设计
wpf
鑫 源20 小时前
C#源码开源, 获取macOS 电池数据,包括iOS (xamarin开发)
c#·.net·xamarin
爱吃奥利奥_wen1 天前
面向对象三板斧:封装、继承、多态,到底在“装“什么、“承“什么、“变“什么?
开发语言·经验分享·c#
wflynn2 天前
GitHub 今日推荐|SCSKiller:游戏着色器预编译消除卡顿
开源·c#·github·nvidia·amd·directx12
一个天蝎座 白勺 程序猿2 天前
IoTDB集群扩容:从慌乱到从容的经验分享
数据库·wpf·时序数据库·iotdb
疯狂的维修2 天前
关于用C#写电机起保停控制
开发语言·c#
用户788477316343 天前
门店核销闭环:小程序扫码 + .NET 幂等接口 + WPF 看板,一天能跑通
微信小程序·c#
samble3 天前
从零搭建灌装监控系统(十三):报警系统设计,生命周期与通知
c#·wpf·mvvm·modbus·工业控制