Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

WPF / Silverlight Quick Tip: INotifyPropertyChanged for indexer

Pete Brown - 08 March 2010

WPF and Silverlight allow you to bind to property indexers by string key or numeric index. For example:

<TextBox Text="{Binding [field1], Mode=TwoWay}" />
<TextBox Text="{Binding Fields[field1], Mode=TwoWay}" />
<TextBox Text="{Binding [15], Mode=TwoWay}" />

If you're creating the data source for those (for example, you are building your own ObservableDictionary), you may wonder how on earth you fire the appropriate INotifyPropertyChanged.PropertyChanged event to let the binding system know that the item with that field name or index has changed.

The binding system is looking for a property named "Item[]", defined by the constant string Binding.IndexerName. In your own setter, the notify would look something like this:

public string this[string key]
{
    get { return _items[key]; }
    set
    {
        _items[key] = value;

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(Binding.IndexerName);
    }
}

The case of "Item[]" is important; if you change the case, the binding system won't recognize it. Use the constant string.

Updated 2010-03-10: A commenter pointed out (thanks Oleg!) that there's a constant for this. Binding.IndexerName. I've updated the code example above to reflect that.

       
posted by Pete Brown on Monday, March 8, 2010
filed under:        

17 comments for “WPF / Silverlight Quick Tip: INotifyPropertyChanged for indexer”

  1. Tom McKearneysays:
    I haven't tested this, but does this only notify to bindings of this particular key, or does it fire for every element in the Item[] collection? It seems like it would cause all references to Item[] to update, which could be problematic for some
  2. Petesays:
    @Steve

    Correct.

    @Oleg

    You just totally sucked the fun out of the room. Where has all the magic gone? :)

    (Thanks. Always better to use the constant, I just didn't even bother to look for one. I'll update the blog post with it)

    Pete
  3. GAsays:
    Hi,

    Great post.

    Using SL4 RTM with VS 2010 RTM, the line below produces an error stating it cannot find "IndexerName" property. Is this only available in WPF?

    Binding.IndexerName
  4. Petesays:
    @GA

    Thanks.

    Looks like that's the case. Use the string "Item[]" instead. I originally had that in the sample, but someone pointed out Binding.IndexerName as a better practice. Forgot to re-test in Silverlight.

    Pete
  5. Bradsays:
    Try using "Item[" + key + "]" to avoid getting hammered by binding when using the broader "Item[]".

    However, "Item[]" is a convenient property name to fire an event with to get all item-based properties in one go.
  6. Bradsays:
    @Pete

    I'm using SL 4.0.50917.0 (release 28 Sep 2010) and works as I mentioned. I haven't tried it with WPF.

    I setup a view using four properties bound using the item indexer method. From the model side, I push a value into "Name" and fire "Item[]", all four properties get read. I repeat and fire "Item[Name]", only "Name" is read.
  7. Cameron Peterssays:
    @Brad --

    Unfortunately I can't get "Item[PropName]" to work with WPF. Any hints on how to make it work? Or perhaps this feature is not yet supported on WPF stack...
  8. RichardPemsays:
    The dosage of REVATIO injection [url=http://generic-tretinoin.se/]tretinoin cream online[/url] does not should be changed for physical body weight. These occasions were frequently of early beginning with peak occurrence taking place during the initial 2 weeks of treatment, and [url=http://generic-viagra-soft-tabs.nu/]viagra soft tabs 50 mg[/url] passing with a typical period of 16 days. In case you are currently making use of other medications stated, [url=http://cheapabilify.science/]canadian abilify[/url] your medical professional requires to learn about it and offer appropriate recommendations for you to stay clear of communications. There have actually [url=http://erythromycinonline.webcam/]erythromycin online[/url] been unusual postmarketing records of non-fatal and deadly hepatic failure in individuals taking statins, including simvastatin. If serious liver injury regarding clinical symptoms and/or hyperbilirubinemia or jaundice happens throughout treatment with Zocor, immediately interrupt therapy. , if an alternate etiology is not discovered do not restart Zocor.. Keep in mind that ALT could rise from muscular tissue, therefore ALT rising with CK might suggest myopathy [see Warnings as well as Precautions (5. It slows the development and also spread [url=http://buyerythromycin.webcam/]where to buy erythromycin[/url] of the herpes infection in the body. Henoch-Schonlein purpura has actually been stated. An instance of subacute cutaneous lupus erythematosus has also been [url=http://genericcafergot.party/]generic cafergot[/url] reported. Professional studies in pediatric [url=http://generic-diclofenac.se/]generic diclofenac[/url] clients included a placebo-controlled trial in pubertal boys of teen age with gynecomastia and also a single-arm trial in girls with McCune-Albright Syndrome as well as modern bright puberty. If [url=http://wellbutrinonline.science/]wellbutrin xl[/url] you think you need to use this or some other medicine a lot more usually that every 24 hrs - contact your physician yet do not make any sort of choices based upon your very own judgment. Viagra (additionally known under its universal label - sildenafil) is utilized [url=http://propranololonline.party/]20 mg propranolol[/url] to deal with impotence (impotence). Your doctor may additionally [url=http://zoloft.webcam/]zoloft[/url] advise a lower dose to minimize prospective negative side effects. In kids needing antibiotic therapy, administration [url=http://buystromectol.science/]buy stromectol online[/url] of 7.5 mg/kg q12h doses of clarithromycin as the suspension normally caused steady-state top plasma attentions of 3 to 7 mcg/mL for clarithromycin and also 1 to 2 mcg/mL for 14-OH clarithromycin. On the 7th day, breastmilk levels of the medicine as well as metabolites were gauged at an average [url=http://buycipro.science/]buy cipro online[/url] 2.5 hrs (range 1 to 12 hrs) after the last dosage. You can purchase Avodart over the counter from any one of the most effective drug stores showcased [url=http://antabuseonline.science/]antabuse online[/url] on our comparison page, without requirement to visit a physician each time to obtain a refill. This testimonial recommends that CCBs do not [url=http://erythromycin.party/]erythromycin[/url] represent a major teratogenic threat. Those are not all the drugs most likely to interact with Cafergot, [url=http://ventolin.webcam/]ventolin[/url] which is why if there are other ones you think are necessary to point out, you need to always do that. Prednisolone can be suggested for clients with low corticosteroid degrees that should have the equilibrium of [url=http://cheaplevitra.webcam/]levitra[/url] that all-natural happening chemical restored. Do not share Sildenafil with other individuals - although [url=http://generic-alli.biz/]alli[/url] their signs could be the same to the ones you have, taking this drug without previously speaking with a medical professional is stuffed with major safety effects. Your quantity might be [url=http://cheapallopurinol.party/]allopurinol[/url] altered occasionally to make certain you are profiting from the therapy. You do not have to state to your medical professional such mild side results as spinning [url=http://buydoxycycline.science/]doxycycline online pharmacy[/url] feeling, weakness, puking, dizziness, nausea or vomiting or mild itching. One comparative research study has shown that losartan lessens cough in individuals who have a record of cough associated regarding ACE inhibitors. However, situations of coughing, including rechallenges, have actually been linked with losartan usage in postmarketing research. Upper breathing system infection has been stated [url=http://buymethotrexate.webcam/]methotrexate cost[/url] in 7.9 % of treated clients, compared to 6.9 % regarding inactive medicine.

Comment on this Post

Remember me