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

  • Home
  • SEARCH
  • 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 6761109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:08:49+00:00 2026-05-26T14:08:49+00:00

I have the following (condensed) silverlight xaml for a view / viewmodel: <UserControl x:Class=MyView>

  • 0

I have the following (condensed) silverlight xaml for a view / viewmodel:

<UserControl x:Class=MyView>    
    <UserControl.Resources>
        <MyViewModel x:Name="MyViewModel"/>
    </UserControl.Resources>

    <Grid DataContext="{Binding Source={StaticResource MyViewModel}}">
<UserControl>

Hopefully this looks familiar to you all.

However, I would like to create 2 instances of this same view user control, but to pass in a parameter to the view model to allow me to have slightly different view model data based on a property which I pass into the view model. Something like:

<UserControl x:Class=MyView>    
    <UserControl.Resources>
        <MyViewModel x:Name="MyViewModel" Filter="Some value set at a higher level"/>
    </UserControl.Resources>

    <Grid DataContext="{Binding Source={StaticResource MyViewModel}}">
<UserControl>

The problem is that I can’t hard code the Filter parameter inside the user control, but need to set it at a higher level. Is there a way to obtain the filter parameter from higher up via binding, and what would the syntax look like. I was hoping something like the following:

Either directly from the parent something like:

<MyView>
    <MyView.ViewModel Filter="All">
</MyView>
<MyView>
    <MyView.ViewModel Filter="Some">
</MyView>

Or from user control looking upwards, something like:

<UserControl.Resources>
    <MyViewModel x:Name="MyViewModel" Filter="{Binding FilterTypeFromDataContextHigherUpTheTree}"/>
</UserControl.Resources>

but I don’t know if is is possible to directly refer to the static resource view model from the parent in order to set a property, or what the syntax would look like.

I also don’t know if there is an easier way to do this, as I suspect my approach is not very elegant.

The real question is how can I pass a parameter into a view model which is a static resourd

  • 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-05-26T14:08:49+00:00Added an answer on May 26, 2026 at 2:08 pm

    Though it’s possible to store the ViewModel as a resource, we usually see the ViewModel set as the DataContext of a view. Then, if someone needs to access the ViewModel through the View, they just cast the DataContext to the proper ViewModel type and access it’s properties directly.

    If you want to go the Binding route for your Filter you have a couple of choices. You can set the Bnding Soure to RelativeSource.TemplatedParent if the child control is placed inside of the parent control in a template (or Style). You could also use ElementName, but only if the named element is inside of the same scope (in your example above, the named element would have to be somewhere inside of MyView).

    The final option I can think of would be to expose a DependencyProperty for the filter on MyView and then set the binding of the ViewModel to that property. This would effectively bubble the filter so that it’s accessible outside of MyView, but I don’t like this approach at all because it’s adding properties to the View just for the sake of passing them to the ViewModel. That should never happen. The ViewModel should always be accessible independantly of the View, which is why I recommend exposing it through the DataContext property (inferred) or through a custom property specifically for the ViewModel (explicit).

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

Sidebar

Related Questions

I have the following condensed form <form name=thecars> <select name=cars> <option value=mustang>Mustang</option> <option value=pinto>Pinto</option>
I have following mapping definitions: <class name=Role table=Role optimistic-lock=version > <id name=Id type=Int32 unsaved-value=0
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following classes. class A { public: void fun(); } class B: public
I have the following tables (condensed for readability): Call ID CallerName CallerTime Categories ID
I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)
I have following code in html: <div> <div style=float:left;margin:0.5em> <span class=title>Label1</span><br/> <input type=text name=name1
I have following XML string: <persons> <person> <name>Someone</name> <age>27</age> </person> <person> <name>Otherone</name> <age>43</age> </person>
i have following html structure. <p class=expandableContent ><span class=label>Computer and Laptop Repairs</span></p> <div style=clear:

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.