C# 从ListView项目内部获取父DataContext

C# 从ListView项目内部获取父DataContext,c#,listview,binding,windows-phone-8.1,winrt-xaml,C#,Listview,Binding,Windows Phone 8.1,Winrt Xaml,我的设想: 在当前页面中,我设置了一个DataContext,它包含两个属性,第一个是页面标题问题,第二个是项目列表回复 我正在将回复绑定到ListView的ItemsSource属性: 您可以让您的答复对其答复的问题具有不可变的引用。或者,假设您的DataContext是一个SomePageViewModel,让它的回复列表不是您的域模型Reply,而是ReplyViewModel,它知道回复和问题的相关部分 这实际上并没有回答您的问题,但这是一个更好的设计,它确实解决了问题 在不更改回复域模

我的设想: 在当前页面中,我设置了一个DataContext,它包含两个属性,第一个是页面标题问题,第二个是项目列表回复

我正在将回复绑定到ListView的ItemsSource属性:

您可以让您的答复对其答复的问题具有不可变的引用。或者,假设您的DataContext是一个SomePageViewModel,让它的回复列表不是您的域模型Reply,而是ReplyViewModel,它知道回复和问题的相关部分

这实际上并没有回答您的问题,但这是一个更好的设计,它确实解决了问题

在不更改回复域模型的情况下构建此域的一种方法如下:

// 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; }
}

ElementName解决方案仅在与所引用的命名元素处于相同的命名上下文中时才起作用,这在DataTemplates中很好地工作。您正在从Usercontrol内部尝试,这是一个不同的命名上下文。我认为目前还没有一个解决方案可以仅仅通过Xaml和绑定来实现这一点。您可能需要在回复中添加家长导航属性。这正是Bart van Nierop刚才建议的答案:只有修改回复对象结构才能这样做?我无法找到一种构建功能性ViewModel的方法我希望找到一种不会有重复属性的方法,但似乎不可能。另一种选择是对当前开放的问题使用全局变量…从我发现的情况来看,如果不改变您的模型,确实是不可能的,但这不是一件坏事。首先,它使您的用户控件可以轻松地在应用程序的其他部分重用,因为它不依赖于其父代的DataContext,并且它使您能够更清晰地区分业务逻辑和表示。另一方面,这意味着传递了更多的数据,而且由于回复列表最多可以包含90项,如果有很多这样的值对象复制,可能会更严重,您可能会考虑只将问题本身添加到RePyVIEW模型中,而不是将其添加到内容中。90对某些对象的引用实际上不应该对性能产生明显的影响。
<UserControl ....>
    <Grid Background="#33FFFFFF">
         <Grid.Resources>
              <local:converter1 x:Key="key" Question="{Binding Tag.Question, ElementName=responseList}"/>
         </Grid.Resources>
    </Grid>
</UserControl>
// 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; }
}