1

我在这里开始发疯了,我是 WPF 的新手,这似乎很难。我来自 MVC,我想做的是在 Razor 中快速操作。我正在尝试绑定以下内容:

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

该属性作为属性:

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

我还想将此绑定到“子”数据模板。如何绑定可枚举类的可枚举属性?我正在玩 ItemsControl,但在已设置的 TreeView 上不断出现错误,或者所有内容都呈现到我网格的一个单元格中,彼此重叠。在遇到以下代码的问题后,我最终转向了 ItemsControl,但这我更加困惑:(。

我真的很感激在这件事上的任何指导,因为我无法理解故障。我得到了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>
4

1 回答 1

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>
于 2013-05-15T18:07:40.530 回答