1

There are 2 classes. One is a parent that contain a list of the second class as a property. The second class has a list also.

The problem is the application doesn't bind the list inside the second class.

The application will display a list of grid that bind from the second class list (in the parent class) and each grid will display information from the list inside the second class.

Now each grid doesn't bind value

Class Parent{
   public List<NestedClass> Child { get; set; }
    .
    .
    .
}

Class NestedClass{
    public ObservableCollection<SomeParameter> Params{ get; set; }
    public string Name
    {
        get
        {
            return "Hello world";                
        }
    }

    .
    .
    .
}

The xaml as below.

    <ItemsControl  HorizontalContentAlignment="Stretch" ItemsSource ="{Binding Child}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding Child.Params}" >
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="80"/>
                                    <ColumnDefinition Width="80"/>
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0">
                                    <TextBlock Text="{Binding Count}" />
                                </Grid>
                                <Grid Grid.Column="1"></Grid>
                                <Grid Grid.Column="2"></Grid>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>                    
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

The output window show as below

System.Windows.Data Error: 40 : BindingExpression path error: 'Params' property not found on 'object' ''NestedClass' (HashCode=16626097)'. BindingExpression:Path=Params; DataItem='NestedClass' (HashCode=16626097); target element is 'ItemsControl' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

Edited#1 Added the xaml

Edited#2 Added the output message

Edited#3 Added the name property in the NestedClass

4

2 回答 2

0

无论您是否尝试过以及是否有效,正确的方法都是@groupgrip提到的:

<ItemsControl HorizontalContentAlignment="Stretch" ItemsSource="{Binding Child}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Params}">
                ...
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

DataContextDataTemplate被设置为Child集合中类型为 的项目NestedClass。这个类有一个名为 的属性Params,所以我们可以Bind直接从DataTemplate. 还应该有一个Count在该类中命名的属性,因为您也试图BindDataTemplate.

无论哪种方式,自行解决这些类型的问题的方法是阅读 Visual Studio 的输出窗口中的错误内容。您经常会收到类似“无法在该对象上找到此属性”之类的错误,这应该可以帮助您追踪并解决任何问题。

于 2013-11-13T09:09:47.617 回答
0

对于“父”类,使用HierarchicalDataTemplate,对于子类 - DataTemplate。在资源中定义这两个模板。

<Grid>
    <Grid.Resources>
        <HierarchicalDataTemplate DataType="{x:Type vm:Parent}" ItemsSource="{Binding Children}">... </>
        <DataTemplate DataType="{x:Type vm:Child}">...</>
    </Grid.Resources>
    <ItemsControl ItemsSource="{Binding Parents}">...</>
</Grid>
于 2013-11-13T09:53:48.923 回答