Reputation: 23
I am attempting to create a column in a DataGrid which is nothing more than a rectangle with a solid color. This column is defined with the following XAML.
<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>
The problem is that although my data source for this grid does have a property named "Color", the solidcolorbrush as defined in my XAML is not utilizing this value. As can be seen, I do have other columns which are bound also, and do bind appropriately at runtime.
Upvotes: 1
Views: 1322
Reputation: 23
Being new to WPF, the problem wasn't obivous at the beginning. My custom data object to which this DataGrid was being bound has a property named "Color" of type System.Drawing.Color. As WPF utilizes System.Windows.Media.Color, I was forced to create a value converter class of my own. Here it is below.
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;
}
}
}
Then, I only had to add this as a resource to my user interface and use the following syntax upon binding to the WinForms "Color"...
<UserControl.Resources>
<conv:WinFormsColorToWpfColorConverter x:Key="WinFormsColorToWpfColor" />
</UserControl.Resources>
<SolidColorBrush Color="{Binding Path=Color, Converter={StaticResource ResourceKey=WinFormsColorToWpfColor}}" />
Upvotes: 1
Reputation: 65054
Sorry, I can't reproduce this one.
I created a new WPF Application in Visual C# Express 2010, copied your DataGrid into MainWindow.xaml, and modified the code in MainWindow.xaml.cs to contain the following:
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; }
}
}
When I ran the application, I got a grid with coloured rectangles down the first column.
(In a real application, the class Colored
would implement INotifyPropertyChanged
and colors
would be an ObservableCollection
instead of a List
. For the purposes of this demo, we'll leave such details aside.)
Upvotes: 0