RadColorPicker and EventToCommand

5 posts, 0 answers
  1. Charles Bates
    Charles Bates avatar
    45 posts
    Member since:
    Feb 2010

    Posted 03 Jun 2011 Link to this post

    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....
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 08 Jun 2011 Link to this post

    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
  3. DevCraft banner
  4. kamlnedra
    kamlnedra avatar
    1 posts
    Member since:
    Jun 2011

    Posted 24 Jun 2011 Link to this post

    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!
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 29 Jun 2011 Link to this post

    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
  6. Michael
    Michael avatar
    24 posts
    Member since:
    Sep 2010

    Posted 07 Sep 2011 Link to this post

    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);
            }
        }
    }
Back to Top
DevCraft banner