Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>XamDataGrid cells contain editors and for values that should be presented as text, editor is XamTextEditor. </p> <p>You must modify template of XamTextEditor and provide your own like in the following code:</p> <pre><code>&lt;Window x:Class="XamDataGridApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:igDP="http://infragistics.com/DataPresenter" xmlns:igEditors="http://infragistics.com/Editors" xmlns:local="clr-namespace:XamDataGridApp" Title="Colorful XamDataGrid" SizeToContent="WidthAndHeight"&gt; &lt;igDP:XamDataGrid FieldLayoutInitialized="OnFieldLayoutInitialized"&gt; &lt;igDP:XamDataGrid.DataSource&gt; &lt;!-- Replace this with your data source. --&gt; &lt;local:DataSourceMock/&gt; &lt;/igDP:XamDataGrid.DataSource&gt; &lt;igDP:XamDataGrid.Resources&gt; &lt;local:XamTextEditorConverter x:Key="XamTextEditorConverter" x:Shared="True"/&gt; &lt;Style TargetType="igEditors:XamTextEditor" BasedOn="{StaticResource {x:Type igEditors:XamTextEditor}}"&gt; &lt;Style.Setters&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type igEditors:XamTextEditor}"&gt; &lt;Border x:Name="MainBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"&gt; &lt;ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"&gt; &lt;ContentPresenter.Content&gt; &lt;MultiBinding Converter="{StaticResource XamTextEditorConverter}"&gt; &lt;Binding Path="DisplayText" RelativeSource="{RelativeSource TemplatedParent}"/&gt; &lt;Binding RelativeSource="{RelativeSource TemplatedParent}"/&gt; &lt;/MultiBinding&gt; &lt;/ContentPresenter.Content&gt; &lt;/ContentPresenter&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style.Setters&gt; &lt;/Style&gt; &lt;Style TargetType="igEditors:XamTextEditor" x:Key="CustomClassXamTextEditorStyle" x:Shared="True" BasedOn="{StaticResource {x:Type igEditors:XamTextEditor}}"&gt; &lt;Setter Property="ValueToDisplayTextConverter" Value="{x:Static local:CustomClassToStringConverter.Instance}"/&gt; &lt;Setter Property="ValueToTextConverter" Value="{x:Static local:CustomClassToStringConverter.Instance}"/&gt; &lt;/Style&gt; &lt;/igDP:XamDataGrid.Resources&gt; &lt;/igDP:XamDataGrid&gt; &lt;/Window&gt; </code></pre> <p>Following IMultiValueConverter is used to generate XamTextEditor content imperatively:</p> <pre><code>using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; using Infragistics.Windows.DataPresenter; using Infragistics.Windows.Editors; namespace XamDataGridApp { class XamTextEditorConverter : IMultiValueConverter { private static readonly IList&lt;Brush&gt; colors = new Brush[] { Brushes.Red, Brushes.Green, Brushes.Blue }; public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var displayText = (string)values[0] ?? string.Empty; // Context that can be used for custom coloring. var editor = (XamTextEditor)values[1]; var dataItemPresenter = editor.Host as DataItemPresenter; // Context that can be used for custom coloring. var dataValue = editor.Value; var dataItem = dataItemPresenter != null ? dataItemPresenter.Record.DataItem : null; var textBlock = new TextBlock() { TextWrapping = editor.TextWrapping, TextAlignment = editor.TextAlignment }; for (int i = 0; i &lt; displayText.Length; ++i) textBlock.Inlines.Add(new Run(displayText[i].ToString()) { Foreground = colors[i % colors.Count] }); return textBlock; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } } </code></pre> <p>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.</p> <p>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.</p> <hr> <p><strong>UPDATE:</strong></p> <p>In order for your converter to be used by XamDataGrid, you must define CellValuePresenterStyle in FieldSettings (not FieldLayouts) like in the following XAML snippet:</p> <pre><code>&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.FieldSettings&gt; &lt;igDP:FieldSettings CellValuePresenterStyle="{StaticResource cellTemplate}"/&gt; &lt;/igDP:XamDataGrid.FieldSettings&gt; &lt;/igDP:XamDataGrid&gt; </code></pre> <p>With this code snippet, you specify default CellValuePresenterStyle for all cells in a XamDataGrid.</p> <p>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 <a href="http://help.infragistics.com/Help/NetAdvantage/WPF/2011.2/CLR4.0/html/InfragisticsWPF4.DataPresenter.v11.2~Infragistics.Windows.DataPresenter.DataPresenterBase~FieldLayoutInitialized_EV.html" rel="nofollow">FieldLayoutInitialized</a> event like in the following code:</p> <pre><code>private void OnFieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e) { foreach (var field in e.FieldLayout.Fields) if (field.DataType == typeof(Model.CustomClass)) { field.Settings.EditorType = typeof(XamTextEditor); // Set Editor style in which display converter and edit converter are specified. field.Settings.EditorStyle = (Style)((FrameworkElement)sender).FindResource("CustomClassXamTextEditorStyle"); } } </code></pre> <p>MainWindow.xaml from above is updated to reflect these changes.</p> <p>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):</p> <pre><code>class CustomClassToStringConverter : IValueConverter { public static CustomClassToStringConverter Instance = new CustomClassToStringConverter(); public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return ((CustomClass)value).Text; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return new CustomClass() { Text = (string)value }; } } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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