2008-12-11 23 views

回答

2

我認爲使用IValueConverter是合適的解決方案。您可以製作一個將字符串十六進制值轉換爲Color的HexConverter。該鏈接應該讓你開始。

6

你會想要使用一個樣式綁定ListViewItem的背景到該行的項目。該項目是一個ListViewItem的默認的DataContext所以這應該是簡單的:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid.Resources> 
     <x:Array x:Key="colors" Type="{x:Type sys:String}"> 
      <sys:String>Red</sys:String> 
      <sys:String>Yellow</sys:String> 
      <sys:String>#0000FF</sys:String> 
     </x:Array> 
    </Grid.Resources> 
    <ListView ItemsSource="{StaticResource colors}"> 
     <ListView.Resources> 
      <Style TargetType="{x:Type ListViewItem}"> 
       <Setter Property="Background" Value="{Binding .}"/> 
      </Style> 
     </ListView.Resources> 
    </ListView> 
</Grid> 

而是結合整個項目,你會綁定到BACKGROUNDCOLOR,但它應該是類似於以上。你必須使用帶有綁定的轉換器作爲前綴「#」,這是內置BrushConverter將其解析爲十六進制的信號。

相關問題