Wpf DataGridTemplateColumn颜色
我试图在DataGrid中创建一个列,它只不过是一个带有纯色的矩形。此列使用以下XAML定义Wpf DataGridTemplateColumn颜色,wpf,data-binding,binding,wpfdatagrid,Wpf,Data Binding,Binding,Wpfdatagrid,我试图在DataGrid中创建一个列,它只不过是一个带有纯色的矩形。此列使用以下XAML定义 <DataGrid x:Name="MeasurementGrid" AutoGenerateColumns="False" AlternatingRowBackground="Azure" SelectionUnit="FullRow" HeadersVisibility="Column" VerticalGridLinesBrush="AntiqueWh
<DataGrid x:Name="MeasurementGrid" AutoGenerateColumns="False"
AlternatingRowBackground="Azure" SelectionUnit="FullRow"
HeadersVisibility="Column" VerticalGridLinesBrush="AntiqueWhite"
HorizontalGridLinesBrush="AntiqueWhite">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Rectangle Width="16" Height="16">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=Color, Mode=OneWay}" />
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Energy" Width="75" Binding="{Binding Path=Energy}"></DataGridTextColumn>
<DataGridTextColumn Header="Field Size" Width="100" Binding="{Binding Path=FieldSize}"></DataGridTextColumn>
<DataGridTextColumn Header="Type" Width="200" Binding="{Binding Path=FriendlyType}"></DataGridTextColumn>
<DataGridTextColumn Header="Depth" Width="75" Binding="{Binding Path=Depth}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
问题是,尽管我的这个网格数据源有一个名为“Color”的属性,但是在我的XAML中定义的solidcolorbrush没有使用这个值。可以看出,我还有其他一些列也被绑定了,并且在运行时进行了适当的绑定。对不起,我无法复制这个列 我在Visual C#Express 2010中创建了一个新的WPF应用程序,将您的DataGrid复制到MainWindow.xaml中,并修改了MainWindow.xaml.cs中的代码以包含以下内容:
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace MyNamespace
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Color> colors = new List<Color>() { Colors.Red, Colors.Green, Colors.Blue };
MeasurementGrid.ItemsSource = colors.Select(color => new Colored() { Color = color }).ToList();
}
}
public class Colored
{
public Color Color { get; set; }
}
}
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Media;
名称空间MyNamespace
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表颜色=新列表(){colors.Red,colors.Green,colors.Blue};
MeasurementGrid.itemsource=colors.Select(color=>newcolored(){color=color}).ToList();
}
}
公共类彩色
{
公共颜色{get;set;}
}
}
当我运行应用程序时,我得到了一个网格,第一列下面有彩色矩形
(在实际应用程序中,
Colored
类将实现INotifyPropertyChanged
,colors
将是一个可观察的集合,而不是一个列表。在本演示中,我们将把这些细节放在一边。)很抱歉,我无法复制这个
我在Visual C#Express 2010中创建了一个新的WPF应用程序,将您的DataGrid复制到MainWindow.xaml中,并修改了MainWindow.xaml.cs中的代码以包含以下内容:
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace MyNamespace
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Color> colors = new List<Color>() { Colors.Red, Colors.Green, Colors.Blue };
MeasurementGrid.ItemsSource = colors.Select(color => new Colored() { Color = color }).ToList();
}
}
public class Colored
{
public Color Color { get; set; }
}
}
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Media;
名称空间MyNamespace
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表颜色=新列表(){colors.Red,colors.Green,colors.Blue};
MeasurementGrid.itemsource=colors.Select(color=>newcolored(){color=color}).ToList();
}
}
公共类彩色
{
公共颜色{get;set;}
}
}
当我运行应用程序时,我得到了一个网格,第一列下面有彩色矩形
(在实际应用程序中,Colored
类将实现INotifyPropertyChanged
,colors
将是一个可观察的集合
,而不是一个列表
。在本演示中,我们将把这些细节放在一边。)对于WPF来说是一个新手,这个问题在一开始时并不明显。此DataGrid绑定到的自定义数据对象具有名为“Color”的属性,类型为System.Drawing.Color。由于WPF使用System.Windows.Media.Color,我不得不创建自己的值转换器类。在下面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
namespace WpfTchartTest.Converters
{
class WinFormsColorToWpfColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
System.Drawing.Color color = (System.Drawing.Color)value;
System.Windows.Media.Color converted = System.Windows.Media.Color.FromArgb(
color.A, color.R, color.G, color.B);
return converted;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
}
然后,我只需将其作为资源添加到我的用户界面,并在绑定到WinForms“Color”时使用以下语法
作为WPF的新手,这个问题在一开始并不明显。此DataGrid绑定到的自定义数据对象具有名为“Color”的属性,类型为System.Drawing.Color。由于WPF使用System.Windows.Media.Color,我不得不创建自己的值转换器类。在下面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
namespace WpfTchartTest.Converters
{
class WinFormsColorToWpfColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
System.Drawing.Color color = (System.Drawing.Color)value;
System.Windows.Media.Color converted = System.Windows.Media.Color.FromArgb(
color.A, color.R, color.G, color.B);
return converted;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
}
然后,我只需将其作为资源添加到我的用户界面,并在绑定到WinForms“Color”时使用以下语法
我解决了自己的问题。问题是我绑定到的属性“Color”的类型是System.Drawing.Color。我必须创造一个价值转换器。我解决了自己的问题。问题是我绑定到的属性“Color”的类型是System.Drawing.Color。我必须创建一个价值转换器。