2017-04-21 44 views
0

是否可以將顏色更改爲xaml中給定的文本單元格?listview中元素的不同顏色

在我的情況下,我想根據給定的值將顏色更改爲給定的元素。 我試圖在視單元中使用backgroundcolor,但這會影響所有元素。

有什麼辦法隻影響一個給定的單元格?

這是我的XAML代碼:

<StackLayout> 
     <ListView x:Name="postListView" ItemSelected="Handle_ItemSelected"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell > 
         <StackLayout Orientation="Horizontal" BackgroundColor="Green"> 
          <Label Text="{Binding Description}"/> 
         </StackLayout> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 
+1

您可以將bgcolor綁定到你的數據的值,並使用ValueConverter的數據值轉換成適當的顏色 – Jason

回答

0

你必須看看這個回購

Binding with listview

在ViewCell我有一個StackLayout使用綁定

slView.SetBinding(StackLayout.BackgroundColorProperty, "BackgroundColor"); 

在MyModel我有一個字符串

public string BackgroundColor { get; set; } 

然後,在視圖模型,我設置顏色

List.Add(new Model { Description = "D1", Cost = 10.0, Qty = 1, BackgroundColor = "#9ac16e", TextColor = "#001833" });