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 6341487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:02:40+00:00 2026-05-24T20:02:40+00:00

Within my project I have a Listbox which uses a datatemplate. Within this data

  • 0

Within my project I have a Listbox which uses a datatemplate. Within this data template I have a button. When the listbox generates results the item source for this listbox is set to some property collection let’s call it Results[0].

The issue I am having is that when I click the button to call a method from the view model the method cannot be found because the call is looking to the context of the listbox and not the root view. I’m using SimpleMVVM toolkit which users a Locator similar to the MVVMLight toolkit.

One approach I took was to explicitly set the data context on the button by declaring the viewmodel in the user control resources and staticlly setting it.

<UserControl.Resources>
    <formatter:HighlightConverter x:Key="FormatConverter" />
    <vml:SearchViewModel x:Key="vm" />
</UserControl.Resources>

and then the button contains

<HyperlinkButton HorizontalAlignment="Left"
    Click="Button_Click"
    Content="{Binding Type}"
    Style="{StaticResource ListBoxtTitleHyperlink}">
 <i:Interaction.Triggers>
     <i:EventTrigger EventName="Click">
        <ei:CallMethodAction MethodName="GetDetailID" TargetObject="{Binding Source={StaticResource vm}}" />
     </i:EventTrigger>
 </i:Interaction.Triggers>
</HyperlinkButton>

This works in the fact that I can now access the method but it instantiates a new view model vs allowing me to access the root viewmodel of the view. Ergo I loose any properties that I may have had in the previous view model so I cannot pass them to the method as parameters.

My implementation may be off here. So I am open to suggestions. In scenarios such as this what is the best approach to have a button within a listbox data template call a method from the view model and pass the method parameters that are derived from the selected listbox item?

To see the full code implementation you can download the sample project from SkyDrive Folder

Update I’m starting a bounty on this question as it has me stumped. Feel free to download the sample project for reference. For clarity the intent of this question is to learn how to accomplish the following
1. Select a row from listbox
2. The selectionchanged event will set a property to the text value displayed in the UI (RecordID two way binding using Inotify
3. Click on the button within the item template and call the method stored within the ViewModel using interaction triggers and displaying in a messagebox the RecordID property value.

Steps I and 2 are done. Where I am stuck is in understanding how to get the button which is part of the listbox item template to locate the root view model and call the method of that VM without instantiating a new ViewModel that would reset all previously stored properties.

Thanks in advance

  • 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-24T20:02:41+00:00Added an answer on May 24, 2026 at 8:02 pm

    Add the resource programmatically. The StaticResource binding might complain at design time but at runtime it should just work.

    The UserControl has a Resources property which returns a reference to a ResourceDictionary. You can add the ViewModel to this and the effect will be the same as your Xaml example except that you can reuse the existing ViewModel.

    Assuming that your MVVM framework has already populated the DataContext of the UserControl with the ViewModel then you could use C# code similar to the following to set up the resource.

    this.Resources.Add("vm", this.DataContext);
    

    If the DataContext is already set within the UserControl’s constructor, then it could go there. Otherwise, you will need to find a hook that is called later in the UserControl’s life cycle.

    Edit: Having looked at your code. I would suggest the following modifications.

    1. Do not set either the DataContext or the “vm” StaticResource in XAML.
    2. Use the following code as the constructor of the TemplateView class.

    Code:

    public TemplateView()
    {
        var templateViewModel = new TemplateViewModel();
        this.DataContext = templateViewModel;
        this.Resources.Add("vm", templateViewModel);
        InitializeComponent();
    }
    

    There are a couple of constraints here that led me to this solution. First is that the resource must be added prior to InitializeComponent. Second is that the templateViewModel must be available before the resource can be added.

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

Sidebar

Related Questions

Within my Subversion project I have a few directories that contain other open source
I have a Setup project (MSI) in VS2008 which installs my project. Within the
I have a Maven project within which I need execute two code generation steps.
Within my project I have gotten this error: Error 1 The type or namespace
I have Aptana installed within Eclipse. I'm working on a project with the dojo
I have a listbox with a bunch of contols in each list item. <ListBox
I have a solution with two projects within: Company.Project.vbproj Company.Project.Tests.vbproj Within the Company.Project.vbproj assembly,
I have an asp.net c# project and need to open another page (within the
Say I've got a project under git version control. Within that project, I have
I have created a Library Project which I import into another project. In that

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.