I also asked this at Infragistics, but don't know how to format my code there, so here it is properly formatted.
My goal is to present a table of structured data with the text of each cell using multiple colors. I have a typeconverter which will convert the data stored in a custom class to a label or textblock containing several text elements with different colors. The data is provided in a datatable (any method that works will be fine) and each value is correctly applied to a cell.
The problem is that instead of using my TypeConverter it uses the ToString method, which I override so I know the model correct model data is mapped on the grid cell by cell. Also the ControlTemplate properties that I'm using are not applied, which tells me the ControlTemplate is not being used.
A concern is it may not be possible to have text where different letters have different colors in a datagrid. If so, is there another way that can be done while still having a good user experience and keeping the design in the xaml file (which is hard with a grid).
As I understand it my code should define a custom CellValuePresenter, can anyone please help me to apply it?
I'm posting my relevant code here. Most of it is obfuscated so please don't focus on spelling errors.
<Window x:Class="ViewName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LocalNamespace"
xmlns:ViewModel="clr-namespace:LocalNamespace.ViewModel"
xmlns:model="clr-namespace:LocalNamespace.Model"
xmlns:igDP="http://infragistics.com/DataPresenter"
>
<Window.Resources>
<local:Converter x:Key="converter" />
<ViewModel:ViewModelLocator x:Key="viewModelLocator" />
<Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="cellTemplate" x:Name="cellTemplate" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Label
Content="{Binding Converter={StaticResource converter}}"
Width="200"
MaxWidth="600"
MinHeight="20"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel Name="stackPanel">
<igDP:XamDataGrid Name="DifferenceGrid" DataSource="{Binding Source={StaticResource viewModelLocator}, Path=ViewModel.Model}"
ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:FieldLayout.Fields>
<igDP:Field>
<igDP:Field.Settings>
<igDP:FieldSettings
CellValuePresenterStyle="{StaticResource cellTemplate}">
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</StackPanel>
</Window>
class ViewModelLocator
{
private static ViewModel viewModel = new ViewModel();
public ViewModel ViewModel
{
get
{
return viewModel;
}
}
}
public class ViewModel
{
private DataTable model;
public DataTable Model
{
get
{
return this.model;
}
private set
{
this.model = value;
}
}
[global::System.ComponentModel.TypeConverter(typeof(Model.CustomClass))]
public class Converter : TypeConverter, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (this.CanConvertTo(targetType))
{
return this.ConvertTo(value);
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (this.CanConvertFrom(targetType))
{
return this.ConvertFrom(value);
}
else
{
return null;
}
}
public new bool CanConvertFrom(Type sourceType)
{
// Textboxes don't need to be converted back.
return sourceType == typeof(Model.CustomClass);
}
public new bool CanConvertTo(Type destinationType)
{
return destinationType == typeof(Model.CustomClass);
}
public object ConvertTo(object value)
{
return this.ConvertCustomClassToTextBlock(value);
}
public new object ConvertFrom(object value)
{
return this.ConvertCustomClassToTextBlock(value);
}
private object ConvertCustomClassToTextBlock(object value)
{
TextBlock text = new TextBlock();
Label cell = new Label();
// Construct the TextBlock.
cell.Context = text;
return text; // Or cell, whatever works.
}
}
XamDataGrid cells contain editors and for values that should be presented as text, editor is XamTextEditor.
You must modify template of XamTextEditor and provide your own like in the following code:
Following IMultiValueConverter is used to generate XamTextEditor content imperatively:
ContentPresenter inside XamTextEditor template must be data-bound to editor's DisplayText property because XamDataGrid cells are virtualized and with the binding we instruct XamDataGrid to regenerate ContentPresenter content when cell virtualization kicks in (when CellValuePresenter gets reused to present another value). Also, because of the data-binding, content will be regenerated even if it is changed outside of XamDataGrid.
MultiBinding is used to pass a context (XamTextEditor) from which data value and data item can be extracted and used in a coloring logic. If context is not required, simple binding to DisplayText with IValueConverter can be used instead.
UPDATE:
In order for your converter to be used by XamDataGrid, you must define CellValuePresenterStyle in FieldSettings (not FieldLayouts) like in the following XAML snippet:
With this code snippet, you specify default CellValuePresenterStyle for all cells in a XamDataGrid.
If you want to specify that your CustomClass column uses XamTextEditor and that all other columns use their own default editor, you can do it by handling FieldLayoutInitialized event like in the following code:
MainWindow.xaml from above is updated to reflect these changes.
Here is also code for CustomClassToStringConverter which I used to convert CustomClass to string and back again when displaying and editing CustomClass in XamDataGrid (I added Text property to CustomClass since I didn't know how you extract text from it):
Thanks I got it working now. Had to provide a seperate datasource with textblocks, don't need the multibinding now. But I don't know if the MVVM thing still applies, it's a pretty ugly solution.