2011-02-17 36 views
1

我,如果這有可能不知道。我有這個數據網格包含3列和2行。我想要的是根據我輸入的數據更改我的數據網格上的URL列的背景。如何編程修改DataGrid行的顏色在WPF?

這裏是我的XAML代碼示例:

<DataGrid 
    x:Name="mylistview" 
    BorderBrush="#FF373737" 
    Background="Black" 
    VerticalGridLinesBrush="#FF232323" 
    HorizontalGridLinesBrush="#FF212121"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="URL" Foreground="{Binding rowColor}" Binding="{Binding url}"/> 
     <DataGridTextColumn Header="Version" Foreground="White" Binding="{Binding version}"/> 
     <DataGridTextColumn Header="Valid" Foreground="White" Binding="{Binding valid}"/> 
    </DataGrid.Columns> 
</DataGrid> 

我用來填補了行DataGrid類:

public class myData 
{ 
    public string url {get; set;} 
    public string version{get; set;} 
    public string valid{get; set;} 

    //this should be the value that changes the bg color 
    public string rowColor{ get; set; } 
} 

這裏是我的方式行添加到控制:

myData data1 = new myData(); 
data1.url = "http://google.com"; 
data1.valid = "yes"; 
data1.version = "1"; 
data1.rowColor = "#FF000000"; 
this.mylistview.Items.Add(data1); 

data1.url = "http://yahoo.com"; 
data1.valid = "no"; 
data1.version = "1"; 
data1.rowColor = "#000000FF"; 
this.mylistview.Items.Add(data1); 

我有兩個元素。每行應該有自己的顏色。第一個應該是紅色 ,第二個應該是藍色的。我真的不知道如何讓控件使用rowColor值。

什麼我需要做的就是這個工作?

回答

3

rowColor不應該返回一個字符串(我會因爲它是公共的它重命名爲RowColor),剛剛返回的實際顏色刷代替。 (你的顏色不對順便說,第一個是不透明的黑色,第二個是透明的藍色)

例如

public Brush rowColor { get; set; } 
data1.rowColor = Brushes.Red; 
data2.rowColor = Brushes.Blue; 

如果你真的需要從字符串進行解析時,您可以使用解析XAML中時所使用的value converter或標準轉換。

除此之外,我不確定是否將這些屬性綁定到列的Foreground將起作用。

相關問題