//xaml
<Window x:Class="WpfApp232.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
WindowState="Maximized"
xmlns:local="clr-namespace:WpfApp232"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:MultiColorConverter x:Key="multiColorConverter"/>
</Window.Resources>
<Grid>
<Grid.Background>
<MultiBinding Converter="{StaticResource multiColorConverter}">
<Binding Path="RedByte" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="GreenByte" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="BlueByte" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Slider x:Name="redSlider"
Value="{Binding RedByte,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Minimum="0"
Maximum="255"
Grid.Column="0"
Background="Red"/>
<Slider x:Name="greenSlider"
Value="{Binding GreenByte,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Minimum="1"
Maximum="255"
Grid.Column="1"
Background="Green"/>
<Slider x:Name="blueSlider"
Value="{Binding BlueByte,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Minimum="2"
Maximum="255"
Grid.Column="2"
Background="Blue"/>
</Grid>
</Window>
//cs
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp232
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
OnValueChanged();
}
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propName));
}
}
private byte redByte;
public byte RedByte
{
get { return redByte; }
set
{
if (value != redByte)
{
redByte = value;
OnPropertyChanged();
OnValueChanged();
}
}
}
private byte greenByte;
public byte GreenByte
{
get { return greenByte; }
set
{
if (value != greenByte)
{
greenByte = value;
OnPropertyChanged();
OnValueChanged();
}
}
}
private byte blueByte;
public byte BlueByte
{
get { return blueByte; }
set
{
if (value != blueByte)
{
blueByte = value;
OnPropertyChanged();
OnValueChanged();
}
}
}
private void OnValueChanged()
{
Application.Current?.Dispatcher.BeginInvoke(new Action(() =>
{
this.Title = $"Red:{redByte},Green:{greenByte},Blue:{blueByte}";
}));
}
}
public class MultiColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 3 &&
byte.TryParse(values[0]?.ToString(), out byte r) &&
byte.TryParse(values[1]?.ToString(), out byte g) &&
byte.TryParse(values[2]?.ToString(), out byte b))
{
return new SolidColorBrush(Color.FromRgb(r, g, b));
}
return Brushes.Gray;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
![]()
![]()
![]()