2012-04-05 109 views
0

我使用Windows Phone的C#和Silverlight。如何將XAML中的Canvas元素綁定到代碼後面的Canvas屬性?

我有一個ListBox的listBoxOfItemsA其ItemsSource屬性綁定到一個ObservableCollection observableCollectionOfItemsA。在ListBox中的每個TypeA項目都使用ListBox.ItemTemplate繪製,如XAML中所示。 注意Canvas元素。沒有它,一切都很好。現在,我已將TypeA的每個實例與TypeB的0個或更多實例相關聯。 TypeB的每個實例都定義了一個HighlightColor。 我需要某種方式來處理ListBox中的每個TypeA項目,以顯示與其關聯的所有TypeB項目的HighlightColor。我正在考慮爲每個ListBox項目添加一個薄的Canvas元素,以便在0或更多區域中水平劃分它(0將僅留下黑色背景),並且在該Canvas中繪製與TypeB的實例關聯的許多矩形ListBox項目(以便所有這些矩形的總寬度不變)。

問題:我必須在「????」位置,以便我可以將Canvas屬性(在TypeA中)與ListBox.ItemTemplate中的Canvas元素綁定?

謝謝。

編輯20120406_0202:每個類型A實例可以被映射到一個以上的TypeB實例同時。例如:每個TypeA是貨幣的移動(從一個賬戶到另一個賬戶,在某個日期並且屬於某個類別等),並且每個TypeB是每個TypeA可能滿足或可能不滿足的一組條件例如,它的日期在一定範圍內,其目的地帳戶等於某個等等)。對於每個TypeA,我想一目瞭然地顯示它滿足哪些條件。滿意的每一組都會出現一個額外的顏色。

public class TypeA 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public Canvas CanvasForItemsB {...} // I would like something like this, here, and to bind it to the Canvas element in the XAML. 
} 

public class TypeB 
{ 
    public SolidColorBrush HighlightColor {...} 
} 

ObservableCollection<TypeA> observableCollectionOfItemsA; 
listBoxOfItemsA.ItemsSource =observableCollectionOfItemsA; 

<ListBox x:Name="listBoxOfItemsA"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding Id, Mode=OneWay}" /> 
       <TextBlock Text="{Binding Name, Mode=OneWay}" /> 
       <Canvas ????="{Binding CanvasForItemsB, Mode=OneWay}" Width="480" Height="10" Background="Black" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

回答

0

如果你想突出你的列表框元素(一個或多個),它很可能是簡單到只需將您的ItemTemplate周圍的StackPanel一個邊界,然後設置在邊框的背景結合屬性。那麼你不必擔心將一堆Canvas對象存儲在內存中,或者不得不將它們注入到ListBox中。

+0

KodeKreachor,listBoxOfItemsA中的每個項目都可以映射爲MORE THAN ONE顏色。 – Telaclavo 2012-04-05 23:40:13

+0

很酷,所以看起來你可以在列表項的視圖模型上更改背景顏色屬性的畫筆顏色值,然後該視圖將反映更新後的顏色。 – KodeKreachor 2012-04-05 23:46:12

+0

我的意思是同時有多種顏色。 – Telaclavo 2012-04-06 00:02:00

相關問題