
運行效果見上圖, 可以根據(jù)單元格中的值來設(shè)置顏色,100%為紅色,其他為綠色。
實現(xiàn)方法:用列模板綁定數(shù)據(jù)源中定義的顏色數(shù)據(jù)項。
其中列模板用Border加TextBox構(gòu)成,代碼如下:
private static DataTemplate MakeCellTemplate(MyColumn col)
{
try
{
DataTemplate dt1 = new DataTemplate();
StringBuilder xaml = new StringBuilder();
xaml.Append("<DataTemplate ");
xaml.Append("xmlns='http://schemas.microsoft.com/client/2007' ");
xaml.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
xaml.Append(">");
xaml.Append("<Border ");
xaml.Append("BorderThickness='0.5' BorderBrush='LightGray' Background='Orange'>");
xaml.Append("<TextBox ");
xaml.Append("IsReadOnly='True' Text='{Binding " + col.Name + "}' Foreground='Black' BorderBrush='White' BorderThickness='0' Background='{Binding " + col.colorValue + "}'");
xaml.Append("/>");
xaml.Append("</Border>");
xaml.Append("</DataTemplate>");
dt1 = (DataTemplate)XamlReader.Load(xaml.ToString());
return dt1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
return null;
}
}
posted on 2010-12-07 16:17
天書 閱讀(2539)
評論(0) 編輯 收藏 引用