在ItemsControl中将View用作DataTemplate时未设置DataContext [英] DataContext not set when using View as DataTemplate in ItemsControl

查看:64
本文介绍了在ItemsControl中将View用作DataTemplate时未设置DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewModel的ObservableCollection,我想绑定到包含关联子Views的ItemsControl。当我将ViewModels添加到集合中时,ItemsControl中会生成适当数量的子视图。但是,每个生成的视图的DataContext为null。如果我插入子视图,则它可以正常工作。那么,我该怎么做才能将我的子视图的DataContext设置为ViewModels?

I have an ObservableCollection of ViewModels that I'd like to bind to an ItemsControl containing the associated child Views. When I add ViewModels to my collection, an appropriate number of child Views are generated in the ItemsControl. However, the DataContext for each of the generated views is null. If I inline my child view, it works correctly. So, what do I need to do to set the DataContext for my child views to my ViewModels?

以下是我的父级ViewModel中的相对位:

Here's the relavent bits in my parent ViewModel:

    public ObservableCollection<ChildViewModel> Numbers { get; set; }

    public ParentViewModel()
    {
        Numbers = new ObservableCollection<ChildViewModel>();
    }

    private void ShowNumbers()
    {
        foreach (var num in Enumerable.Range(0, number))
        {
            var childView = new ChildViewModel(number.ToString());
            Numbers.Add(childView);
        }
    }

父视图中的相关位:

        <ItemsControl ItemsSource="{Binding Numbers, UpdateSourceTrigger=PropertyChanged}">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type vm:ChildViewModel}">
                    <v:ChildView />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

儿童视野:

<UserControl x:Class="TestWpfApp.Views.ChildView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:prism="http://prismlibrary.com/"             
         prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
    <Label Content="{Binding NumberString}" Width="30" Height="30" BorderThickness="1" BorderBrush="Black" HorizontalAlignment="Center"/>
</Grid>
</UserControl>

Child ViewModel:

The Child ViewModel:

public class ChildViewModel : BindableBase
{
    private string numberString;
    public string NumberString
    {
        get
        {
            return numberString;
        }
        set
        {
            SetProperty(ref numberString, value);
        }
    }

    public ChildViewModel() { }

    public ChildViewModel(string number)
    {
        NumberString = number;
    }
}

很明显,我的配置错误,但是我可以

Obviously, I have something misconfigured, but I can't for the life of me figure out what.

仅供参考,我正在使用棱镜库

FYI I am using the Prism Library

推荐答案

WPF会自动将 ItemsControl 中的项目容器元素的 DataContext 设置为适当的项目实例,以便可以将其继承到 ItemTemplate 中。显然,当您设置 prism:ViewModelLocator.AutoWireViewModel 属性时,该机制将被禁用。

WPF automatically sets the DataContext of an item container element in an ItemsControl to the appropriate item instance, so that it can be inherited into the ItemTemplate. Apparently this mechanism is disabled when you set the prism:ViewModelLocator.AutoWireViewModel property.

因此,只需将其从您的ChildView的XAML:

So, just remove it from your ChildView's XAML:

<UserControl x:Class="TestWpfApp.Views.ChildView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/">
    <Grid>
        <Label Content="{Binding NumberString}" Width="30" Height="30"
               BorderThickness="1" BorderBrush="Black" HorizontalAlignment="Center" />
    </Grid>
</UserControl>






作为一般规则,UserControl绝不应明确设置其自己的 DataContext 既不直接也不通过 AutoWireViewModel 之类的机制设置,因为这样可以有效地防止继承 DataContext 来自其父控件。


As a general rule, a UserControl should never explicitly set its own DataContext, neither directly nor by a mechanism like AutoWireViewModel, because that effectively prevents inheriting a DataContext from its parent control.

这篇关于在ItemsControl中将View用作DataTemplate时未设置DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆