Hi,
This is my solution for the problem:
MainWindow.cs:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525" x:Name="window" removed="LightBlue">
<Window.Resources>
<local:BrushToIntConverter x:Key="BrushToIntConverter" />
</Window.Resources>
<DockPanel>
<Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource ResourceKey=BrushToIntConverter}, ConverterParameter=R}"/>
<Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource ResourceKey=BrushToIntConverter}, ConverterParameter=G}"/>
<Slider DockPanel.Dock="Top" Minimum="0" Maximum="256" Value="{Binding Path=Background, ElementName=window, Mode=TwoWay, Converter={StaticResource ResourceKey=BrushToIntConverter}, ConverterParameter=B}"/>
</DockPanel>
</Window>
Three silders for R,G,B.
And the Brush to Int Converter:
[ValueConversion(typeof(System.Windows.Media.Brush), typeof(int))]
public class BrushToIntConverter : IValueConverter
{
protected System.Windows.Media.Color m_Color = System.Windows.Media.Colors.White;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
System.Windows.Media.SolidColorBrush brush = value as System.Windows.Media.SolidColorBrush;
m_Color = brush.Color;
switch (parameter as string)
{
case "R":
return m_Color.R;
case "G":
return m_Color.G;
case "B":
return m_Color.B;
}
return 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int intValue = (int)Math.Round((double)value);
byte r = m_Color.R;
byte g = m_Color.G;
byte b = m_Color.B;
switch (parameter as string)
{
case "R":
r = (byte)intValue;
break;
case "G":
g = (byte)intValue;
break;
case "B":
b = (byte)intValue;
break;
}
m_Color = new System.Windows.Media.Color() { R = r, G = g, B = b, A = m_Color.A };
return new System.Windows.Media.SolidColorBrush(m_Color);
}
}