0

I'm developing a MVVM app with an RGB selector. I have 3 sliders for each channel and would like to use routed events to catch the ValueChanged event on each slider

<StackPanel Grid.Row="0" Grid.Column="1" Slider.ValueChanged="DoSmth">
    <Slider Value="{Binding R}" Minimum="0" Maximum="255" />
    <Slider Value="{Binding G}" Minimum="0" Maximum="255" />
    <Slider Value="{Binding B}" Minimum="0" Maximum="255" />
</StackPanel>

Now, this beeing a MVVM app, I'd like to use Commands for this. However I can't see to find a way to send a Command without having to assign it to each slider separately. I read a bit on "Routed Commands" but that didn't give me a solution either.

How can i accomplish this?

Christos
  • 53,228
  • 8
  • 76
  • 108
Xaser
  • 2,066
  • 2
  • 22
  • 45
  • Why not add the same command to each slider? You don't need any parameters, as you're already binding R,G and B, so the method handling the command can work off of those properties rather than deal with a parameter. – Mashton Oct 06 '14 at 16:08
  • It doesn't even need a Parameter, the code to be executed is the same for all sliders in the Stackpanel. Also I'm all about scalability, I will need many more sliders for other parameters, thats why I don't want any redundant code. – Xaser Oct 06 '14 at 18:19
  • I said it doesn't need a parameter, and yes the code is the same for all sliders. This is why I said bind the **same command to each slider** and also why I said **you don't need any parameters**. And the code isn't redundant: when you move the red slider you want something to happen. You also want the same thing to happen when you move the green or blue slider, so how are you going to convey that something should happen unless you wire up a command on those sliders? – Mashton Oct 06 '14 at 20:17
  • Exactly as I mentioned: [Routed Events](http://msdn.microsoft.com/en-us/library/ms742806%28v=vs.110%29.aspx). If I could just wire the event to a Command by just setting one parameter on each slider, that might still be allright, if I however have to copy paste the same trigger each time (as shown here [here](http://stackoverflow.com/questions/6205472/mvvm-passing-eventargs-as-command-parameter)), I see my code going very messy when more sliders are added – Xaser Oct 07 '14 at 05:11

1 Answers1

1

If you insist on using an ICommand then you should wrap the Slider.ValueChanged event using an Attached Property. You can find out how to do that in my answer to the What's the best way to pass event to ViewModel? question.

However, you really shouldn't need to use any ICommand... surely you can just create a new Color each time one of the data bound properties change in the view model? This example enables the user to change the colour without using any ICommands:

<StackPanel Grid.Row="0" Grid.Column="1">
    <Slider Value="{Binding R}" Minimum="0" Maximum="255" />
    <Slider Value="{Binding G}" Minimum="0" Maximum="255" />
    <Slider Value="{Binding B}" Minimum="0" Maximum="255" />
    <Rectangle HorizontalAlignment="Stretch" Height="100">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Color}" />
        </Rectangle.Fill>
    </Rectangle>
</StackPanel>

In view model:

private byte r = 127, g = 127, b = 127;
public byte R
{
    get { return r; }
    set { r = value; Color = Color.FromArgb((byte)255, R, G, B); NotifyPropertyChanged("R"); }
}
public byte G
{
    get { return g; }
    set { g = value; Color = Color.FromArgb((byte)255, R, G, B); NotifyPropertyChanged("G"); }
}
public byte B
{
    get { return b; }
    set { b = value; Color = Color.FromArgb((byte)255, R, G, B); NotifyPropertyChanged("B"); }
}
private Color color = Colors.Black;
public Color Color
{
    get { return color; }
    set { color = value; NotifyPropertyChanged("Color"); }
}
Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • yeah I suppose I actually could do that... I feared a cyclic dependency / update because in the `set` of `Color`I already call an update on the sliders, thats why i set the backing property in the colors `set`handler directly... however I might add some faders that don't affect the color shown, thus killing this solution. I'll give the other solution a go and report back. – Xaser Oct 06 '14 at 18:24