0

My scenario: In the current page I set a DataContext which consists in two properties, the first (page heading) Question, the second (a list of items) Replies.

I'm binding Replies to the ItemsSource property of a ListView:

<ListView x:Name="responseList" ItemsSource="{Binding replies}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <local:CustomControl />
            </DataTemplate>
        </ListView.ItemTemplate>       
    </ListView>

Until this point there are no problems. Inside that Custom Control, I need to retrieve also some properties of the Question element (which is outside the ListView), binding those properties into the XAML of the CustomControl.

I looked at this but with no luck since FindAncestor is not recognized in WinRT: WPF Databinding: How do I access the "parent" data context?

This other one returns nothing as DataContext: How to access Parent's DataContext in Window 8 store apps

<UserControl ....>
    <Grid Background="#33FFFFFF">
         <Grid.Resources>
              <local:converter1 x:Key="key" Question="{Binding Tag.Question, ElementName=responseList}"/>
         </Grid.Resources>
    </Grid>
</UserControl>
Community
  • 1
  • 1
fillobotto
  • 3,698
  • 5
  • 34
  • 58
  • The ElementName Solution will only work when you are in the same naming context as the named element you are referencing, which works fine in DataTemplates. You are trying that from inside a Usercontrol, which is a different naming context. I don't think there is a solution currently to do this just via Xaml and Binding. You probably have to add a parent navigation property to your Replies. – Kai Brummund Feb 04 '15 at 14:21
  • Which is exactly what Bart van Nierop just suggested as an answer. :) – Kai Brummund Feb 04 '15 at 14:22

1 Answers1

0

You can have your Replies have a(n immutable) reference to the question they are replies to. Or, perhaps nicer, assuming your DataContext is a SomePageViewModel, have its list of replies not be of your domain model Reply but instead of a ReplyViewModel which knows both about the reply and the relevant parts of the question.

This doesn't actually answer your question, but is much better design and it does solve the problem.

One way this could be structured without changing your Reply domain model is as follows:

// Domain Model example
class Question
{
    public String Text { get; set; }
    public String Author { get; set; 
    public IEnumerable<Reply> Replies { get; set; }
}

// Domain Model example
class Reply
{
    public String Author { get; set; }  
    public String Text { get; set; }
}

// DataContext example
class WhatYouUseAsDataContext
{
    public Question Question { get; set; } 

    // Your replies -- this is what your ListView binds to.
    public IEnumerable<ReplyViewModel> Replies { get; set; } 

    public WhatYouUseAsDataContext()
    {
        Question = SomeWayToGetTheQuestion();
        Replies = Question.Replies.Select(reply => new ReplyViewModel()
        {
            Reply = reply,
            QuestionAuthor = Question.Author
        });
    }
}

// Newly introduced viewmodel class
class ReplyViewModel
{
    public Reply Reply { get; set; }
    public String QuestionAuthor { get; set; }
}
Bart van Nierop
  • 4,130
  • 2
  • 28
  • 32
  • This would be possible only modifying `Reply` object structure? I can't figure out a way to bulid a functional ViewModel – fillobotto Feb 04 '15 at 14:23
  • I hoped to find a way that wouldn't have duplicated properties, but seems impossible. The alternative is to use a global variable for the current opened question... – fillobotto Feb 04 '15 at 16:51
  • From what I've found it's indeed not possible (without altering your model), but that is not a bad thing. For one, it makes your user control easily reusable in other parts of your app because it does not depend on its parents' parent `DataContext`, and it gives you a clearer separation between business logic and presentation. – Bart van Nierop Feb 05 '15 at 09:33
  • On the other side, this means a lot more of data passed and, since a list of replies can count up to 90 items, it might be more heavy – fillobotto Feb 05 '15 at 10:57
  • If there are many value-objects that you duplicate this way, you might want to consider to instead just add the `Question` itself to the `ReplyViewModel`, instead of its contents. 90 references to some object really should not have a noticeable performance impact. – Bart van Nierop Feb 05 '15 at 12:55