2012-04-05 45 views
-1

我正在爲以下問題尋找一個很好的代碼解決方案。如何將列表的元素綁定到標籤

我有一個列表,我想綁定到一對標籤。 MyObject類只包含幾個字段(字符串和整數)。該列表包含3個元素。在我看來,我想有以下佈局

  1. 元1
  2. 元2
  3. 元3

哪裏 「元件1」, 「元素2」 和 「元素3」是MyObject類的一個屬性。

注意,視圖在Blend已經定義如下(我不想改變視圖代碼,但如果我沒有選擇...;)):

<Label Content="1." RenderTransformOrigin="0,0.404" Foreground="Black" FontSize="16" VerticalContentAlignment="Bottom" HorizontalContentAlignment="Right" FontFamily="Segoe UI Semibold"/> 
<Label Content="2." RenderTransformOrigin="0,0.404" Foreground="Black" FontSize="16" VerticalContentAlignment="Bottom" HorizontalContentAlignment="Right" Grid.Row="1" FontFamily="Segoe UI Semibold"/> 
<Label Content="3." Margin="0,0,0,1.077" RenderTransformOrigin="0,0.404" Foreground="Black" FontSize="16" VerticalContentAlignment="Bottom" HorizontalContentAlignment="Right" Grid.Row="2" FontFamily="Segoe UI Semibold"/> 
<Label Content="Login bug startup (6 days)" Foreground="Black" FontSize="14.667" VerticalContentAlignment="Bottom" Grid.Column="1"/> 
<Label Content="Setup screen exception (4 days)" Foreground="Black" FontSize="14.667" VerticalContentAlignment="Bottom" Grid.Column="1" Grid.Row="1"/> 
<Label Content="No users available bug (1 day)" Foreground="Black" FontSize="14.667" VerticalContentAlignment="Bottom" Grid.Column="1" Grid.Row="2"/> 

什麼是最好的如何做到這一點?我確信我可以用一些鬆散的代碼來做到這一點,但我更喜歡一個簡潔的解決方案。

非常感謝!

+0

我可以爲列表中的每個元素創建一些屬性,但我確定這不是要走的路。我正在尋找一個完美的解決方案。 – 2012-04-05 09:40:45

回答

0
<ListView ItemsSource={Binding MyObjectsList}> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding Number}" /> 
       <Label Content="{Binding Element}" /> 
      </StackPanel> 
     </DataTempalte> 
    </ListView.ItemTemplate> 
</ListView> 

Number和Element是MyObject的屬性。

+0

有沒有其他的方式來做到這一點與我在我的初始文章中顯示的視圖代碼?使用列表視圖迫使我再次風格和設計視圖... – 2012-04-05 10:50:49

+0

比使用應該將每個標籤按索引綁定到列表。請點擊此鏈接:http://blogs.microsoft.co.il/blogs/davids/archive/2010/04/17/how-to-bind-to-the-index-of-a-collection-in-wpf。 ASPX。 – 2012-04-05 11:11:47

+0

我不想綁定索引。我的觀點已經包含這些標籤。我只想將「元素1」值綁定到每個標籤 – 2012-04-05 11:19:53

相關問題