2011-10-05 80 views
1

我想創建一個WPF應用程序,我有一個顯示帳戶列表的StackPanel。每個帳戶的代碼中都有一個Account對象,存儲在List結構中。我想將這個數據綁定到我的WPF,但我不想要一個列表框。WPF ContentPresenter只顯示列表中的第一項

相反,我爲每個帳戶摘要應該如何顯示定義了一個模板。然後,我想將它們堆疊在一個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> 
+0

幾點意見: 我創建了一個類,帳戶服務,用的方法ListAccounts()返回一個列表結構。我已確認此功能正確生成列表。 – Emily

回答

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> 
+0

+1,請注意,您可能需要增強模板以包含滾動查看器 – cordialgerm

+0

良好通話,爲ItemTemplate添加scrollviewer以及代碼段 – ThomasAndersson

+0

謝謝! 我不需要添加滾動查看器,因爲我的數據肯定會適合窗口。但技術看起來很穩定,我會試試看。 – Emily

相關問題