How can I set a property when an event happens? (selection changed on datagrid)

I have a datagrid, I would like to change the backgrund of the datagrid when the selection changed event is fired.
I can not use data trigger, because is in the case that I expected a value, but in this case is when change, to any value.
An option is to use an event trigger, but all the examples that I have found is to use with an actions, and I just want to set a property.
How could I do that?
I am using MVVM pattern, so I would like to avoid the use in code-behind.
Thank so much.

You could use an event trigger and implement your own TriggerAction<T> that sets the background property. There is an example of how to create a custom TriggerAction available here:
http://stackoverflow.com/questions/942548/setting-a-property-with-an-eventtrigger.
The following example sets the Background property of the DataGrid to a red SolidColorBrush when the SelectionChanged event is fired. It should give you the idea:
<DataGrid x:Name="dgrid"
xmlns:local="clr-namespace:WpfApplication22">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<local:SetPropertyAction PropertyName="Background" TargetObject="{Binding ElementName=dgrid}">
<local:SetPropertyAction.PropertyValue>
<SolidColorBrush>Red</SolidColorBrush>
</local:SetPropertyAction.PropertyValue>
</local:SetPropertyAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
namespace WpfApplication22
public class SetPropertyAction : TriggerAction<FrameworkElement>
public string PropertyName
get { return (string)GetValue(PropertyNameProperty); }
set { SetValue(PropertyNameProperty, value); }
public static readonly DependencyProperty PropertyNameProperty
= DependencyProperty.Register("PropertyName", typeof(string),
typeof(SetPropertyAction));
public object PropertyValue
get { return GetValue(PropertyValueProperty); }
set { SetValue(PropertyValueProperty, value); }
public static readonly DependencyProperty PropertyValueProperty
= DependencyProperty.Register("PropertyValue", typeof(object),
typeof(SetPropertyAction));
public object TargetObject
get { return GetValue(TargetObjectProperty); }
set { SetValue(TargetObjectProperty, value); }
public static readonly DependencyProperty TargetObjectProperty
= DependencyProperty.Register("TargetObject", typeof(object),
typeof(SetPropertyAction));
protected override void Invoke(object parameter)
object target = TargetObject ?? AssociatedObject;
PropertyInfo propertyInfo = target.GetType().GetProperty(
PropertyName,
BindingFlags.Instance|BindingFlags.Public
|BindingFlags.NonPublic|BindingFlags.InvokeMethod);
propertyInfo.SetValue(target, PropertyValue);
For more information about how to hook up event triggers and handle events in MVVM, please refer to my blog post:
http://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/
Please also remember to mark helpful posts as answer.

Similar Messages

  • How can I set it so when people ring my mobile, it...

    How can I set it so when people ring my mobile, it rings me on skype?
    I get 0 signal in my house but I have internet and I need to take calls badly.

    Hi kpet417,
    If you want to add calendar and weather to be seen on Notification Center, then open that up and look at the bottom for the edit button to bring up options to add to it. Take a look at the article below for more information. 
    Mac Basics: Notifications keep you informed
    https://support.apple.com/en-us/HT204079
    Take it easy,
    -Norm G. 

  • How can i set  "Createdby" attribute  When using Custom JheadStart Security

    Hello
    We do not use JASS for Authentication , please help us how can i set createtby attributes with jhs.username in application for any entity object?
    thanks

    See a similar question at History Attributes when using Custom Authentication Type

  • How can i set a password when i open mozilla firefox homepage

    how can i set username and password to the homepage of mozilla firefox

    hello saranghaekijenia, can i ask what you are trying to achieve by setting a password? maybe there is a better way...

  • How can I set the language when sending an e-mail with attachment - 'Html'?

    "Character Broken"
    I made an ALV program, and this convert to "HTML',  and send an e-mail with this.
    Used Process is:
    DATA: gt_abaplist LIKE TABLE OF abaplist,
          gt_html LIKE TABLE OF w3html,
          doc_chng LIKE sodocchgi1,
          objpack LIKE TABLE OF sopcklsti1 WITH HEADER LINE,
          objhead LIKE TABLE OF solisti1 WITH HEADER LINE,
          mail LIKE TABLE OF solisti1 WITH HEADER LINE,
          reclist LIKE TABLE OF somlreci1 WITH HEADER LINE,
          lv_line TYPE i.
    DELETE FROM MEMORY ID '%_LIST'.
    SUBMIT z_template
       AND RETURN EXPORTING LIST TO MEMORY.
    CALL FUNCTION 'LIST_FROM_MEMORY'
      TABLES
        listobject = gt_abaplist
      EXCEPTIONS
        not_found  = 1
        OTHERS     = 2.
    CALL FUNCTION 'WWW_HTML_FROM_LISTOBJECT'
      TABLES
        html       = gt_html
        listobject = gt_abaplist.
    doc_chng-obj_name   = 'URGENT'.
    doc_chng-sensitivty = 'O'.
    doc_chng-obj_descr  = ' '.
    doc_chng-obj_langu  = '3'.
    objhead-line        = ' '.
    APPEND objhead.
    mail[] = gt_html[].
    DESCRIBE TABLE mail LINES lv_line.
    CLEAR: objpack.
    objpack-transf_bin = 'X'.
    objpack-head_start = 1.
    objpack-head_num   = 0.
    objpack-body_start = 1.
    objpack-body_num   = lv_line.
    objpack-doc_type   = 'htm'.
    objpack-obj_descr  = ' '. 
    objpack-doc_size   = lv_line * 255.
    objpack-obj_langu = '3'.
    APPEND objpack.
    CLEAR: reclist.
    reclist-receiver = 'e-mail address'.
    reclist-rec_type = 'U'.
    APPEND reclist.
    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
      EXPORTING
        document_data              = doc_chng 
        put_in_outbox              = 'X'
        commit_work                = 'X'
      TABLES
        packing_list               = objpack
        object_header              = objhead
        contents_bin               = mail
        receivers                  = reclist
      EXCEPTIONS
        too_many_receivers         = 1
        document_not_sent          = 2
        document_type_not_exist    = 3
        operation_no_authorization = 4
        parameter_error            = 5
        x_error                    = 6
        enqueue_error              = 7
        OTHERS                     = 8.
    SUBMIT rsconn01 AND RETURN.
    I want to show  this by "Japanese".
    But the character is broken.
    LIke This:
    u01D1u072C          u0205|            u0205u072C          u0508           QC u04B90m
    How can I set the language?  How can I solve "Charancter Broken" ?
    Plesse, let me know.
    Thank you so much.
    Edited by: Jaime White 999 on Nov 21, 2011 8:45 AM
    Edited by: Jaime White 999 on Nov 21, 2011 8:46 AM

    Hi Jaime,
    Are you saying, you are getting junk characters in the email attachment? Then pls check the following. Since you want to send Japanese fonts, please pass language 'J' in both  objpack-obj_langu = '3' & doc_chng-obj_langu  = '3' instead of '3' please replace it with 'J'.
    Another thing which you may have to check is the SCOT device type. As I understand from my basis colleague, this may also affect your attachment fonts since the emails are sent through SCOT. Go to transaction SCOT-> Settings-> Device types for format conversion. Check whether wrong device type is defined for Japanese there. Hope this helps
    Regards,
    Gokul

  • How can I set the CS5 to Intuos Pro Large for changing brush size and opacity and others?

    How can I set together  to  my PS5 or Ps6 in order to change brush size and opacity at the Intuos Pro Large?
    Nachricht geändert durch Orhan Tarman

    The MTU setting on the interface is the layer 3 packet size. In this case, the MTU is set to 1500, but the largest frame size transmitted on the interface (including layer 2 headers) is 1516. It sounds like you are experiencing another problem at the LES.

  • How can I stop getting emails when adding events to iCal (Mac) or Calendar (iOS)?

    This question could just as well go to the iCloud on my Mac community...
    I put 'I' in the topic for brevity but it's actually my wife who gets emails every time either of us adds or edits events, whether we do it in iCal on either of our Macs or in Calendar on our iOS devices. Presumably this is to do with a setting in iCloud somewhere, but I've searched high and low and can’t find it. I've also compared settings on our Macs and phones and can't find anything that could be the culprit. It's driving her nuts!
    For more information, we share all our calendars so that either of us read and write any of them, and we're both on iOS 5.1 on our iPhones (hers is a 3GS, mine's a 4), and we're both on Mac OS X 10.7.3 Lion (2010 27" iMac and 2011 MacBook Pro respectively).

    this seems to be a new "bug" on iCloud.  It's happened to many users (judging by the posts here).  My wife and I have one shared calendar and we've been happily using it without the email problems (because I had set iCloud NOT to email us whenever an event changed - this was done at icloud.com, a setting there).  Recently we began getting emails.  I suspect a change in iCloud that was in error, because I just checked my setting and the option to have an email sent when an event changes is not checked.

  • How can i set the values to text box by selecting from drop down list.

    hi ,
    i am using struts and jsps. i have a dropdown list of names(getting the values of names from database), i want to get address and phone number of that selected one.how can i get that?

    Hi swarupa,
    Well My advice in this senario wud Be
    Either U can make Use of XmlHttp Object through javascript(AJAX way).
    Or U need to Maintain the State of the entire form and then refresh the page to get those results....
    just for U r reference i am attaching a link which was programmed using ASP @ server side and which made use of XmlHttp Object.....
    http://www.w3schools.com/ajax/ajax_database.asp
    Just try to simulate the samething in your own way....
    REGARDS,
    RAHUL

  • How can i set settings so when i delete emails off my mac they go off my phone and iPad, how can i set settings so when i delete emails off my mac they go off my phone and iPad

    i want my mac to be the controller of the emails on  my iphone 4s and ipad hd---how do i do this?

    That is how Time Machine works.
    It is not going to keep files you delete from your computer forever.
    But you haven't stated how they are stored on the TC.
    If you copy files to the TC they can be stored long term.. or use another type of backup software.
    See Q3 here about storing data and TM on the TC.
    http://pondini.org/TM/Time_Capsule.html

  • IPhoto 8.1.2 how can you set a password on an event or album?

    I am downloading pictures from my iphone 4 to my iphoto on my mac and I wanted to set a password on a certain group of pictures and was wondering how to do that for the iPhoto 8.1.2 version.

    You can't. It's not a feature of iPhoto.
    For photos you want to hide from Mommy, put them in a Library stored on an encrypted disk image.
    Regards
    TD

  • My husband and I share the same I cloud. I want to create a new I cloud for his I phone but it keeps wanting to ask questions about my apple ID and password. I don't want to change my apple ID or password.  How can I set him up a new account w/o changing

    My husband and I have been sharing an I cloud account.  He can't update his phone due to our shared account.  I want to get him a new account and was told to delete the current I cloud account off his phone and just create a new one.  The problem is that it wants to keep accessing the old one.  I don't want my I cloud account changed.  I don't know what to do.  Any advice?

    Did he have find my iPhone enabled under that iCloud account?  If so, add the account back to his phone.  Then in the iCloud account settings, disable find my iPhone first (you will be prompted for your password), then remove the account.
    Now you can create or add a new iCloud account to it using his own AppleID.
    Alternatively, you could log in to your www.icloud.com account in a web browser.  Using the find my iPhone app there, remove his iPhone from your list of associated devices (he will have to power his iPhone off for you to do that).
    After his iPhone is set up for his own iCloud account, on your iPhone, go to
    settings->iCoud->Storage & Backup->Manage Storage and delete his iPhone's backup file to make more room on your own iCloud storage account.

  • How can I set the page that will open, when I open a new tab, to be the same as the homepage?

    How can I set the page that will open, when I open a new tab, to be the same as the homepage?
    An app changed it to some other search engine, before installing the app, I the tab that would open was the homepage I set.
    In my case it is Google, and I want my new tabs to open on Google like they did before the installation.
    thank you,
    Asaf Privman.

    You can middle-click or Ctrl left-click the Home button on the Navigation toolbar to open the Home page in a new tab.
    You can look at one of these extensions:
    * NewTabURL : https://addons.mozilla.org/firefox/addon/newtaburl/
    * New Tab Homepage : https://addons.mozilla.org/firefox/addon/new-tab-homepage/

  • I have bought an iPhone4 and to be able to use it for the first time I connected into a friends itunes. Now when I want to buy itunes stuff on the phone it asks me her password. How can I set up again my itunes account?

    I have bought an iPhone4 and to be able to use it for the first time I connected into a friends' itunes. Now when I want to buy itunes stuff on the phone it asks me her password. How can I set up again my itunes account?
    Now I also have a macbook where I can sync my iphone but it doesn't allow me

    first of all only sync it with 1 computers itune account
    second settings->general->store click the appleID box with her appleID in it and log out

  • How can I set up lightroom 5 in my macbook, so that it becomes my default editing program, so that it starts up when I insert my sd card into the mac

    How can I set up lightroom 5, so that it becomes my main / default editing program & that when I insert my sd card into the mac lightroom starts instead of iPhoto .

    Thanks.  Im new to apple, which preference?
    Sent from my iPhone

  • How can I set up my email so when I delete mail from my Mac it deletes from my iPhone as well?

    How can I set up my email so when I delete mail from my Mac it deletes from my iPhone as well?

    That is a function of your email account, if it is an Imap account it will sync, if it's a POP account, it won't.
    Who provides your email.

Maybe you are looking for

  • Folder view in iTunes

    Is there a way to browse my music files in a folder view rather than one huge list? I just added about 4000 .mp3 files to my library (first time using iTunes) and the only way I know of to add songs to my iPod is to scroll through this huge list wher

  • Export in 24p (curiosity)

    Hi guys, I'm shooting with a panasonic SD5 1080 in Avchd. Since I've a Quad (not intel), I've to import clips through Imovie with my bookpro and then import in G5 in FinalCut6. To improve a better "film" appearence can I export in 24p mode? I've to d

  • How to create a full width tab panel ?

    Hi, I can't make my tab panel as width as the screen, even when I fix it at each border of the composition, is it possible ? Do you know what should I do ? Many Thanks

  • When do I need to incorporate Dispose into my code?

    I'm trying to understand (a) if I ever need to implement Dispose in my classes and (b) if I do, when do I? Part of the reason for asking is because I have implemented Dispose in some of my classes, because I wanted to ensure that collections of items

  • Law-enforcement-rpn.linabirsa.tk

    Hello everybody, from 2 days my browers (firefox, safari and crhome) are the redirect to law-enforcement-rpn.linabirsa.tk . i think it's a malaware but, how i can fix it? How i can remove this malaware? please help matteo