积极答复者
WPF中datagrid加入Ellipse

问题
-
谢谢,进来关注的各位。我想在datagrid里的第一列里面填充一个 Ellipse 首先是这样操作:建一个Model 其中有一列public System.Windows.Media.Color brushColor
{
get { return _brush; }
set {_brush = value; }
}这是datagrid绑定的方式binding = new System.Windows.Data.Binding("brushColor"); binding.Mode = System.Windows.Data.BindingMode.OneWay; DataGridTemplateColumn dgFileCopyColumnDown = new DataGridTemplateColumn(); dgFileCopyColumnDown.SetCurrentValue(System.Windows.FrameworkElement.TagProperty, 1); dgFileCopyColumnDown.Header = "设备状态"; dgFileCopyColumnDown.Width = 100; System.Windows.DataTemplate dtFile = CreateDT(binding); dgFileCopyColumnDown.CellTemplate = dtFile; dgFileCopyColumnDown.Visibility = System.Windows.Visibility.Visible; //dg_Function是datagrid dg_Function.Columns.Add(dgFileCopyColumnDown);
private DataTemplate CreateDT(Binding binding) { System.Windows.DataTemplate dt = new System.Windows.DataTemplate(); System.Xml.Linq.XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; System.Xml.Linq.XElement xDataTemplate = new System.Xml.Linq.XElement(ns + "DataTemplate", new System.Xml.Linq.XAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"), new System.Xml.Linq.XElement(ns + "Ellipse", new System.Xml.Linq.XAttribute("Width", "12"), new System.Xml.Linq.XAttribute("Fill", "{Binding brushColor, Mode=OneTime}"), new System.Xml.Linq.XAttribute("Height", "12"), new System.Xml.Linq.XAttribute("Name", "ellipseBlink")) ); System.Xml.XmlReader xr = xDataTemplate.CreateReader(); dt = System.Windows.Markup.XamlReader.Load(xr) as DataTemplate; return dt; }
结束,但是没有显示任何的东西。应该是颜色写错了。 谢谢您的耐心看完,能不能帮忙一起看看什么问题导致的。
答案
全部回复
-
new System.Xml.Linq.XAttribute("Fill", "{Binding brushColor, Mode=OneTime}"), new System.Xml.Linq.XAttribute("Height", "12"),
这行如果直接绑定一种颜色是可以显示圆形的
new System.Xml.Linq.XAttribute("Fill", System.Windows.Media.Color.FromArgb(255,181,181,181)),new System.Xml.Linq.XAttribute("Height", "12"),
这样可以显示颜色,但是绑定颜色就不行
现在循环数据源是这么处理的:
if (State == "1")
brushColor = Color.FromArgb(255,99,99,99);
else
brushColor = Color.FromArgb(255, 128, 128, 128); -
Fill需要的是一个brush,
Brush brush = new SolidColorBrush(Colors.Red);
还有,下次关于WPF的问题,你可以到WPF论坛去提问,链接是:
http://social.microsoft.com/Forums/zh-CN/wpfzhchs/threads
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.