Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot apply cellValuePresenter/TypeConverter to XamDataGrid cells
    primarykey
    data
    text
    <p>I also asked this <a href="http://www.infragistics.com/community/forums/p/76138/384534.aspx#384534" rel="nofollow">at Infragistics</a>, but don't know how to format my code there, so here it is properly formatted.</p> <p>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.</p> <p>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.</p> <p>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).</p> <p>As I understand it my code should define a custom CellValuePresenter, can anyone please help me to apply it?</p> <p>I'm posting my relevant code here. Most of it is obfuscated so please don't focus on spelling errors.</p> <pre><code> &lt;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" &gt; &lt;Window.Resources&gt; &lt;local:Converter x:Key="converter" /&gt; &lt;ViewModel:ViewModelLocator x:Key="viewModelLocator" /&gt; &lt;Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="cellTemplate" x:Name="cellTemplate" &gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"&gt; &lt;Label Content="{Binding Converter={StaticResource converter}}" Width="200" MaxWidth="600" MinHeight="20" /&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;StackPanel Name="stackPanel"&gt; &lt;igDP:XamDataGrid Name="DifferenceGrid" DataSource="{Binding Source={StaticResource viewModelLocator}, Path=ViewModel.Model}" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible"&gt; &lt;igDP:XamDataGrid.FieldLayouts&gt; &lt;igDP:FieldLayout&gt; &lt;igDP:FieldLayout.Fields&gt; &lt;igDP:Field&gt; &lt;igDP:Field.Settings&gt; &lt;igDP:FieldSettings CellValuePresenterStyle="{StaticResource cellTemplate}"&gt; &lt;/igDP:FieldSettings&gt; &lt;/igDP:Field.Settings&gt; &lt;/igDP:Field&gt; &lt;/igDP:FieldLayout.Fields&gt; &lt;/igDP:FieldLayout&gt; &lt;/igDP:XamDataGrid.FieldLayouts&gt; &lt;/igDP:XamDataGrid&gt; &lt;/StackPanel&gt; &lt;/Window&gt; 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. } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload