WPF ContentPresenter仅显示列表中的第一项

Emi*_*ily 1 data-binding wpf datatemplate

我正在尝试创建一个WPF应用程序,其中我有一个显示帐户列表的StackPanel.每个帐户的代码后面都有一个Account对象,存储在List结构中.我想将此数据绑定到我的WPF,但我不想要一个列表框.

相反,我为每个帐户摘要的显示方式定义了一个模板.然后我想将它们堆叠在StackPanel中并将其称为一天.

问题是数据绑定只从列表中获取第一个项目,就是这样.如何绑定它,以便有效地创建一个很好的格式化数据块的堆栈列表?

以下是相关的WPF代码:

<StackPanel Name="sp_AccountList" Margin="0,0,0,0" VerticalAlignment="Top">
    <StackPanel.Resources>
    <svc:AccountBalanceColorConverter x:Key="accountColorConverter" />
    <Style x:Key="AccountSummaryBackgroundGradient" TargetType="{x:Type StackPanel}">
    <!-- nice formatting code here -->
    </Style>
    <Style x:Key="AccountSummaryNameStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Padding" Value="10,0,0,0" />
        <Setter Property="FontSize" Value="18" />
        <Setter Property="Height" Value="20" />
        <Setter Property="FontFamily" Value="Cambria" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Background" Value="Transparent" />
    </Style>
    <Style x:Key="AccountSummaryBalanceStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Padding" Value="10,0,0,0" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="Height" Value="20" />
        <Setter Property="FontFamily" Value="Cambria" />
        <Setter Property="Background" Value="Transparent" />
    </Style>                    
    <ObjectDataProvider x:Key="accounts"
                        ObjectType="{x:Type svc:AccountService}"
                        MethodName="ListAccounts" />
    <DataTemplate x:Key="AccountSummaryLayout">
    <StackPanel Orientation="Vertical" Style="{StaticResource AccountSummaryBackgroundGradient}">
        <TextBlock Text="{Binding Path=Name}" Style="{StaticResource AccountSummaryNameStyle}" />
        <StackPanel Orientation="Horizontal">
            <TextBlock Foreground="{Binding Path=TotalAccountBalance, Converter={StaticResource accountColorConverter} }" Text="{Binding Path=TotalAccountBalance, Mode=OneWay}" Style="{StaticResource AccountSummaryBalanceStyle}" />
            <TextBlock Foreground="{Binding Path=AvailableAccountBalance, Converter={StaticResource accountColorConverter} }" Text="{Binding Path=AvailableAccountBalance, Mode=OneWay}" Style="{StaticResource AccountSummaryBalanceStyle}" />
        </StackPanel>
    </StackPanel>
    </DataTemplate>
    </StackPanel.Resources>
    <StackPanel Orientation="Vertical">
        <ContentPresenter x:Name="AccountSummaryPresenter" ContentTemplate="{StaticResource AccountSummaryLayout}" Content="{DynamicResource accounts}" />
    </StackPanel>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

Tho*_*son 5

StackPanel没有ItemsSource属性,其子控件不是databindable.

你可以做的是创建一个ItemsControl,它使用StackPanel作为其Itemshost.

     <ScrollViewer>
            <ItemsControl ItemsSource="{Binding Source={StaticResource accounts}}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel IsItemsHost="True" Orientation="Vertical" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                ...
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        <ScrollViewer>
Run Code Online (Sandbox Code Playgroud)