Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:18:48+00:00 2026-06-03T05:18:48+00:00

I am trying to populate a dependency property from XAML. The dependency property is

  • 0

I am trying to populate a dependency property from XAML. The dependency property is an IEnumerable<KeyAction> where KeyAction is a complex type.

<loc:MyType.KeyActions>
    <loc:KeyAction Action="Show" Key="Space" Modifiers="LeftCtrl" />
    <loc:KeyAction Action="Hide" Key="Escape" />
</loc:MyType.KeyActions>

Now, this causes the KeyAction property to be ‘declared twice’ because XAML interprets each item as a candidate for the property, instead of items in a list.

For this to work, it would need to look something like:

<loc:CompletionPopupView.KeyActions>
    <sys:List`KeyAction>
        <loc:KeyAction Action="Show" Key="Space" Modifiers="LeftCtrl" />
        <loc:KeyAction Action="Hide" Key="Escape" />
    </sys:List`KeyAction>
</loc:CompletionPopupView.KeyActions>

And I’d need to add namespaces, and the generic syntax is probably even more terrible, if even doable. Is there any way to make the first example work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:18:49+00:00Added an answer on June 3, 2026 at 5:18 am

    There are two different ways you can make this work. One looks exactly like your first example, but requires changes to your class and doesn’t operate quite the way you’re asking for (which might or might not be a problem to you); the other acts exactly like you’re asking but is a little more verbose. You can decide which one is better for you.

    Option 1: Adding to a collection

    XAML has magic shorthand syntax for initializing collections, using the exact syntax you show in your first example. However, it only works if the property type implements IList. (Yes, that’s really the non-generic IList — not normally a big deal though, all the generic collections that ship with .NET implement both IList<T> and IList.)

    So you could do your first example, but only if your KeyActions property was declared as a type that implements IList. For example, you could change your property to:

    public ObservableCollection<KeyAction> KeyActions { get {...} }
    

    And then just put multiple child elements inside your property, and it would add them to the collection:

    <loc:MyType.KeyActions>
        <loc:KeyAction Action="Show" Key="Space" Modifiers="LeftCtrl" />
        <loc:KeyAction Action="Hide" Key="Escape" />
    </loc:MyType.KeyActions>
    

    This isn’t quite like what you asked for though, because the XAML doesn’t create a new collection — it adds to the existing collection. So if you choose this option, your class needs to instantiate the collection in its constructor (KeyActions = new ObservableCollection<KeyAction>();), so that you don’t get a null reference exception when you start trying to Add elements to it.

    Option 2: Creating a new collection

    If you do need to create a new collection and assign it to your property, that’s doable too. Unfortunately, XAML2006 (the flavor still used by WPF) only supports generics on the root element of the entire document — so you can’t instantiate a List<T> and assign it to a property.

    But that’s okay; you can use the same workaround that WPF does. Just create your own non-generic class that descends from a generic list.

    public class KeyActionCollection : ObservableCollection<KeyAction> {}
    

    Then you can instantiate it in XAML:

    <loc:CompletionPopupView.KeyActions>
        <loc:KeyActionCollection>
            <loc:KeyAction Action="Show" Key="Space" Modifiers="LeftCtrl" />
            <loc:KeyAction Action="Hide" Key="Escape" />
        </loc:KeyActionCollection>
    </loc:CompletionPopupView.KeyActions>
    

    If you choose this option, you can keep your property declared as IEnumerable<KeyAction> if you wish. The custom list is only necessary to populate the list from XAML.

    You could even combine both approaches: make the property read/write and of type KeyActionCollection, instantiate it in your constructor, and then XAML can choose whether to add to the existing collection using the shorthand syntax, or create a new collection; and runtime code could make the same choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iam trying to populate the HyperlinkButton.NavigateUri Property with a string from the properties... But
I'm trying to populate a List of beans from a form: public class Foo
I am trying to populate a variable with an XML response from an ajax
I'm trying to populate this list with strings from the object data. It comes
I'm trying to populate a listview from another class, but I'm geting this error:
I am trying to populate a dojo pie chart from a json array created
I am trying to populate a listview from sqlite database. My code is using
Trying to populate a uitable from xml, xml is already parsed. for (Row* fighter
I'm trying to populate a class object with values from a database table. The
I am trying to populate the textbox inside a usercontrol with value from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.