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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:04:09+00:00 2026-06-18T01:04:09+00:00

I want to display a list of objects in a LongListSelector, and format them

  • 0

I want to display a list of objects in a LongListSelector, and format them with a DataTemplate. To properly use MVVM I’d like to have a ViewModel in this DataTemplate.

Creating of this ViewModel is no problem, but how do I pass the Item to the ViewModel?

I’m using this code:

<Controls:LongListSelector 
     ItemsSource="{Binding MyItems}" Margin="0" HorizontalAlignment="Stretch" 
                           HorizontalContentAlignment="Stretch" >
    <Controls:LongListSelector.DataContext>
        <viewmodel:MyListOfItemsViewModel />
    </Controls:LongListSelector.DataContext>
 <Controls:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="CurTemplate">
                <Grid Margin="10" >
                    <Grid.DataContext>
                        <viewmodel:MyViewModel MyItem="{Binding Path=DataContext,ElementName=CurTemplate}" />
                    </Grid.DataContext>

But alas, the only thing that is set for MyItem is null, and this is never updated to the real value. I found out that later in the process (after the initial setting of MyItem CurTemplate does have a valid DataContext, but this is not sent to my ViewModel. Am I missing something here?

For completeness the code for MyViewModel:

public static DependencyProperty MyItemProperty = DependencyProperty.Register("MyItem", typeof(object), typeof(MyViewModel), new PropertyMetadata("asd", ItemChanged));

    private static void ItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
       System.Diagnostics.Debugger.Break(); // to set when something is set
       // called once, NewValue is null
    }

    public object MyItem
    {
        get
        {
            return (object)GetValue(MyItemProperty);
        }
        set
        {
            SetValue(MyItemProperty, value);
            RaisePropChangeEvent("MyItem");
        }
    }

I did a lot of searching and fiddling around, but I’m pretty sure this is just a minor thing that is missing here. I would be very glad if you could help me out here…

EDIT: Solved

I solved my problem by using {Binding Path=Content,RelativeSource={RelativeSource Mode=TemplatedParent}} as binding for the viewmodel. I have no idea why this works with Content, but not with DataContext…

Thanks for your help, robertftw, your linked post brought me to the right track!

  • 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-18T01:04:10+00:00Added an answer on June 18, 2026 at 1:04 am

    Seems like the problem I had a few days ago:
    Binding does not update usercontrol property correctly MVVM

    public static DependencyProperty MyItemProperty = DependencyProperty.Register("MyItem", typeof(object), typeof(MyViewModel), new PropertyMetadata(string.Empty, ItemChanged));
     private static void ItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
            var obj = d as MyViewModel;
            obj.RaisePropChangeEvent("MyItem");
    }
    

    The problem I had is that the set of MyItem isn’t actually called.

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

Sidebar

Related Questions

I have this situation where I want to display a list of Administration objects
I have a List box that I want to display a list of objects,
I have a list of business objects that I want to display in a
I have a list of objects. I want to display these objects in such
Well i have a list of objects List<UserDC> now i would want to display
I have a List of objects and a ListView where I display this list.
I have a list of objects produced. after click on next I want to
I want to do something like this: <display:table name=${summary${index}}> But it does not work
I want this code to display list of friends of a user, list of
My use case is as follows -- I have a list(ArrayList) of objects, custom

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.