WPF列表在列表中绑定

时间:2013-05-15 18:00:12

标签: c# wpf xaml

我开始疯狂了,我是WPF的新手,这看起来很难。我来自MVC,我想要做的是Razor的一个快照。我试图绑定以下内容:

public ObservableCollection<FundFamilySum> FundFamilyCredits { get; set; }

该属性作为属性:

public ObservableCollection<BrokerCredit> BrokerCreditList { get; set; }

我还想将其绑定到“子”数据模板。如何绑定可枚举类的可枚举属性?我正在使用ItemsControl,但是一直在设置TreeView上的错误,或者所有内容都渲染到我的网格的一个单元格中,彼此在顶部。在遇到以下代码的问题后,我最终转到ItemsControl,但是this让我更加困惑:(。

我真的很感激这方面的任何指导,因为我无法得到细分。我得到了FundFamilyCredit.FundFamilyName来绑定,但我没有得到BrokerCreditList与DataTemplate绑定的约定。
谢谢!

XAML

<ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid HorizontalAlignment="Stretch">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="20"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="2*"></ColumnDefinition>
                                <ColumnDefinition Width="8*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>-->
                            <TextBlock Text="{Binding Path=FundFamilyName}" Grid.Row="0" Grid.Column="0" Background="White" FontSize="14" Foreground="Black"></TextBlock>
                            <ItemsControl ItemsSource="{Binding Path=BrokerCreditList}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="20"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="1*"></ColumnDefinition>
                                                <ColumnDefinition Width="9*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Grid.Row="0" Grid.Column="0" Background="White" Foreground="Black" FontSize="14" Text="{Binding Path=BrokerName}"></TextBlock>
                                            <Rectangle Width="20" Height="20" Grid.Row="0" Grid.Column="1"></Rectangle>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

1 个答案:

答案 0 :(得分:3)

我认为你几乎一切都是正确的。正如我所看到的,唯一不对的是你缺少一行定义,而你没有将内部ItemsControl设置为第二行

  <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20"></RowDefinition>
                             <RowDefinition Height="Auto"></RowDefinition> <!-- NEW ROW HERE-->
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="2*"></ColumnDefinition>
                            <ColumnDefinition Width="8*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>-->
                        <TextBlock Text="{Binding Path=FundFamilyName}" Grid.Row="0" Grid.Column="0" Background="White" FontSize="14" Foreground="Black"></TextBlock>
                      <!--ITEMS CONTROL INTO SECOND ROW-->
                        <ItemsControl Grid.Row="1" ItemsSource="{Binding Path=BrokerCreditList}">  
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="20"></RowDefinition>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="1*"></ColumnDefinition>
                                            <ColumnDefinition Width="9*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Row="0" Grid.Column="0" Background="White" Foreground="Black" FontSize="14" Text="{Binding Path=BrokerName}"></TextBlock>
                                        <Rectangle Width="20" Height="20" Grid.Row="0" Grid.Column="1"></Rectangle>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>