In my View, I have four sliders that represent the four channels of a color.
The color itself is represented as a Vector4
in my View Model.
How do I write a Converter that "splits" the Vector into it's components so each slider changes a single channel of the color?
My idea was to create one Converter for each channel (Below is for the red channel. The other channels are analogue to the Red converter):
public class ColorToMediaRConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, ystem.Globalization.CultureInfo culture)
{
//Makes sense, Gets the Red Component
Vector4 color = (Vector4)value;
return color.X * 255;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//The value is a float here, it does not make sense to make Vector4 out of it
return null;
}
}
Here is the corresponding XAML:
<pt:ColorSlider LeftColor="Black" RightColor="Red" Value="{Binding DiffuseColor,Converter={StaticResource MediaColorRConverter}}" ></pt:ColorSlider>
<pt:ColorSlider LeftColor="Black" RightColor="Green" Value="{Binding DiffuseColor,Converter={StaticResource MediaColorGConverter}}"></pt:ColorSlider>
<pt:ColorSlider LeftColor="Black" RightColor="Blue" Value="{Binding DiffuseColor,Converter={StaticResource MediaColorBConverter}}"></pt:ColorSlider>
<pt:ColorSlider LeftColor="Transparent" RightColor="White" Value="{Binding DiffuseColor,Converter={StaticResource MediaColorAConverter}}"></pt:ColorSlider>
<Rectangle Height="50" Width="50">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding DiffuseColor,Converter={StaticResource MediaColorConverter}}"/>
</Rectangle.Fill>
</Rectangle>
My ViewModel looks like this:
public Vector4 DiffuseColor
{
get
{
return _material.DiffuseColor;
}
set
{
_material.DiffuseColor = value;
NotifyPropertyChanged(nameof(DiffuseColor));
}
}
When I change the sliders, nothing happens, the Setter of DiffuseColor
does not execute.