This is a migrated thread and some comments may be shown as answers.

RadColorPicker and EventToCommand

4 Answers 118 Views
ColorPicker
This is a migrated thread and some comments may be shown as answers.
Charles Bates
Top achievements
Rank 1
Charles Bates asked on 03 Jun 2011, 04:00 PM
Hello,
We are using the GalaSoft MVVM Light toolkit, and would like to use EventToCommand with the RadColorPicker, like this:
<RadColorPicker>
<!-- stuff -->
<
i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ChangeLinkColorCommand}" PassEventArgsToCommand="True"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="SelectedColorChanged">
        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ChangeLinkColorCommand}" PassEventArgsToCommand="True"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
</RadColorPicker>
The Click and SelectionColorChanged handlers receive System.EventArgs, which don't appear to have much information.
Can we use EventToCommand with these events on the RadColorPicker?  How do we find the sender, and things like the SelectedColor property?

Thanks for any tips....

4 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 08 Jun 2011, 04:36 PM
Hi Charles Bates,

The newly selected color should be accessed in the SelectedColorChanged event handler like so:
Color selColor = (sender as RadColorPicker).SelectedColor as Color;
Let us know if you need further assistance on this.



Greetings,
Petar Mladenov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
kamlnedra
Top achievements
Rank 1
answered on 24 Jun 2011, 12:31 PM
i too want to know how to get the "sender" in this case. please reply @ earliest.if i will get i will definitely post it.
kamal_ssharma@yahoo.com

thanks, in advance!
0
Petar Mladenov
Telerik team
answered on 29 Jun 2011, 04:03 PM
Hello kamlnedra,

Unfortunately, you cannot use the SelectedColorChanged and EventToCommand because the EventArgs does not provide info for the sender (in this case you cannot use e.Source or e.OriginalSource). On the other hand, in a typical MVVM scenario , the ViewModel should not know the sender of the events/commands, this is considered bad practice. Let us know if you have further questions.

Best wishes,
Petar Mladenov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Michael
Top achievements
Rank 1
answered on 07 Sep 2011, 11:52 PM
I love your controls, but that's a really lame answer which speaks to poor design of this otherwise awesome control.  I just ran into this problem, so I threw together a quick behavior that solves this problem.  You'll of course need the Blend SDK (or Prism) so you can include a reference to System.Windows.Interactivity.

Usage:

<telerik:RadColorPicker>
    <i:Interaction.Behaviors>
        <local:ForwardColorToCommand Command="{Binding MyCommand}"/>
    </i:Interaction.Behaviors>
</telerik:RadColorPicker>

Code:
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
 
namespace Ows.Modules.HelloWorld
{
    public class ForwardColorToCommand : Behavior<RadColorPicker>
    {
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof (ICommand), typeof (ForwardColorToCommand), new PropertyMetadata(null, (s, e) => { }));
 
        public ICommand Command
        {
            get { return (ICommand) GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
 
        protected override void OnAttached()
        {
            base.OnAttached();
 
            AssociatedObject.SelectedColorChanged += OnColorChanged;
        }
 
        protected override void OnDetaching()
        {
            base.OnDetaching();
 
            AssociatedObject.SelectedColorChanged -= OnColorChanged;
        }
 
        private void OnColorChanged(object sender, System.EventArgs e)
        {
            var picker = sender as RadColorPicker;
            if (picker == null)
                return;
 
            var command = Command;
            if (command == null)
                return;
 
            var newColor = picker.SelectedColor;
            if (command.CanExecute(newColor))
                command.Execute(newColor);
        }
    }
}
Tags
ColorPicker
Asked by
Charles Bates
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
kamlnedra
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Share this question
or