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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:28:10+00:00 2026-05-13T13:28:10+00:00

OK, so the situation is I’m defining an ItemTemplate for a ListBox in a

  • 0

OK, so the situation is I’m defining an ItemTemplate for a ListBox in a ResourceDictionary (Styles.xaml). The ListBoxItem Template looks something like this:

<ControlTemplate TargetType="ListBoxItem">
    <Button Command="{Binding Path=DoSomeCommand}" Content="Test"  />
</ControlTemplate>

Now wherever this template is used, I’d like to have this button’s click bind to an available ViewModel command to handle it.

However this does not work as is, I’ve also tried this:

<ControlTemplate TargetType="ListBoxItem">
    <Button Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DoSomeCommand}" Content="Test"  />
</ControlTemplate>

But still no dice.

A simple example that does work is if you define the template in the control (resources) that is using it, and just use an event handler (the same handler for all generated XAML.

Any ideas or thoughts on the best way to accomplish this? I figure this must be a common scenario: the goal is just to allow the user to interact with the items in the ListBox.

Thanks!

  • 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-13T13:28:10+00:00Added an answer on May 13, 2026 at 1:28 pm

    OK I think I answered my own question :

    The solution seems to be to use ‘nested’ ViewModels here:

    In other words, rather than have my ListBox bind directly to a collection of DTOs/business objects (as I was doing above) I instead created a simple ViewModel to wrap each DTO, and have the command on it, rather than on the original, top-level VM.

    So the bound collection now looks like this:

        TestItems = new ObservableCollection<ItemVM> ()
        {
            new ItemVM(),
            new ItemVM(),
            new ItemVM()
        };
    

    And each ItemVM just wraps the DTO, and has the command:

    public class ItemVM : INotifyPropertyChanged
    {
        public ItemVM ()
        {
            this.MyCommand = new DelegateCommand<string> ( TheCommand );
        }
    
        public ICommand MyCommand { get; private set; } 
        public MyBusinessObject BizObj;
    }
    

    And voila, no need for a RelativeSource, and we have a reusable template complete with commands.

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

Sidebar

Related Questions

Situation is a string that results in something like this: <p>This is some text
Situation I'd like to make an RPC interface easier to use. This is a
Situation: I often run into this problematic and never know how to solve it.
Situation: I am trying to write a efficient query using "LIKE" statement to look
Situation: I am using the Navigation Based Application project template. And my root view
Situation: I have an Xcode project based on the Navigation-Based-Application template. So that means
Situation: I'm implementing a list-like container that supports a Pop() function that should return
Situation: I am working on a Qt4 application constructed in this way (in case
Situation According to this accepted answer , if the " GC 'sees' a cyclic
Situation: text: a string R: a regex that matches part of the string. This

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.