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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:16:04+00:00 2026-05-19T15:16:04+00:00

I have a problem with Commands and Datatemplates in WPF and I don’t know

  • 0

I have a problem with Commands and Datatemplates in WPF and I don’t know if it’s a bug or a normal behavior.

I have a CustomControl containing a listview. This control is used in several views (instances of UserControl). The datatemplate of the listview contained in the control is:

<DataTemplate x:Key="StandardContentDisplayDataTemplate">
    <Grid d:DesignWidth="193.333" d:DesignHeight="128.036" Margin="25">
        <Button Style="{Binding ItemButtonStyle, RelativeSource={RelativeSource AncestorType={x:Type Control:ContentDisplay}}}" 
            Margin="0" 
            VerticalContentAlignment="Stretch" 
            HorizontalContentAlignment="Center"
            Command="{Binding DataContext.ItemTouchCommand, RelativeSource={RelativeSource AncestorType={x:Type Control:ContentDisplay}}}"
            CommandParameter="{Binding Id}"
            Background="{Binding HexadecimalColor, FallbackValue=#FFAAAA}" BorderThickness="0" HorizontalAlignment="Stretch" Padding="0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="3*"/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Name, FallbackValue=Mon Item}" HorizontalAlignment="Center" VerticalAlignment="Center" ToolTipService.ToolTip="{Binding Name}" FontSize="14.667" Margin="0" Grid.Row="0"/>
                <Image Margin="1,0,1,1" Grid.Row="1" VerticalAlignment="Stretch" Source="{Binding ImageUrl, FallbackValue=/logo.png, TargetNullValue=/logont.png}" Stretch="Uniform" />
            </Grid>
        </Button>
    </Grid>
</DataTemplate>

The thing I do is binding the command to the datacontext of the ContentDisplay (which is my CustomControl) that is to say, my ViewModel.

The action linked to this command is a navigation to another view containing the same control and also a command in its ViewModel, bound the same way (It has a total of 3 levels).

When I click the button in the top level view, it navigates correctly to the 2nd level view, displaying other items. But when the ViewModel is instanciated and the Command bound to the items in the listview, the Command is called once again, and again and again.
My theory is that the DataTemplate notifies all of its parents (all instances) when binding the command. But maybe there is a bug somewhere in my code.

Is this behavior normal? and if it is, is there a way to do what I want to achieve in MVVM-respectful way?

Thank you in advance for your responses

  • 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-19T15:16:04+00:00Added an answer on May 19, 2026 at 3:16 pm

    The problem is right here:

     Command="{Binding DataContext.ItemTouchCommand, RelativeSource={RelativeSource AncestorType={x:Type Control:ContentDisplay}}}"
    

    Your Command is bound to the ContentDisplay’s ViewModel. I would recommend taking your DataTemplate and making a UserControl with its own backing ViewModel (with the ItemTouchCommand on that ViewModel). Expose a dependency property on the UserControl that takes the type of the Item that is being displayed. Then you can change your datatemplate to simply hold an instance of that control like so

    <DataTemplate x:Key="StandardContentDisplayDataTemplate">
        <Control:ItemDisplayControl Item="{Binding}"/>
    </DataTemplate>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.