Home Forums WPF controls Xceed DataGrid for WPF Image as Column Title

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #24503 |

    I have a column which its title must be only an image. I known that I have to use a template like the documentation explains : TitleTemplate. But is it possible to have a simple example in which the Template is defined in a resource dictionnary.

    Regards.

    Imported from legacy forums. Posted by Bernard (had 762 views)

    User (Old forums)
    Member
    Post count: 23064

    Sample for an image as a column title.

    First sample with the path of image
    ———————————–

    1. Create the template for the title :

    <DataTemplate x:Key=”SectionIsExecutable_TitleTemplate”>
    <Image Width=”16″ Height=”16″ x:Name=”Image” Source=”c:\path\image.ext”/>
    </DataTemplate>

    2. Use the template in the column :

    <Custom:Column FieldName=”SectionIsExecutable” TitleTemplate=”{StaticResource SectionIsExecutable_TitleTemplate}” Width=”40″ ReadOnly=”True”/>

    Remark :

    The template can be defined in the windows, user control or dictionnary resources.

    <UserControl.Resources>
    <DataTemplate ….
    </DataTemplate>
    </UserControl.Resources>

    Second sample, with a converter which return the image depend on converter parameter
    ————————————————————————————

    1. Create the converter in C# :

    namespace WPFSecurityControls
    {
    public class IConverterImageSectionPageAttributes : IValueConverter
    {
    #region IValueConverter Membres

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    switch (parameter.ToString())
    {
    case “IsExecutable”:
    return CWPFImageHelper.ConvertToBitmapImage(Properties.MainResource.PageExecute);
    break;


    }

    return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    return null;
    }

    #endregion
    }
    }

    2. Map the converter from C# to XAML :

    <local:IConverterImageSectionPageAttributes x:Key=”ConverterImageSectionPageAttributes”/>

    where local is defined as xmlns:local=”clr-namespace:WPFSecurityControls” (‘WPFSecurityControls’ is the namespace where the converter is defined)

    3. Create the template :

    <DataTemplate x:Key=”SectionIsExecutable_TitleTemplate”>
    <Image Width=”16″ Height=”16″ x:Name=”Image” Source=”{Binding Converter={StaticResource ConverterImageSectionPageAttributes}, ConverterParameter=’IsExecutable’}”/>
    </DataTemplate>

    4. Use the template in the column :

    (same as sample 1)

    <Custom:Column FieldName=”SectionIsExecutable” TitleTemplate=”{StaticResource SectionIsExecutable_TitleTemplate}” Width=”40″ ReadOnly=”True”/>

    To insert the image in a cell, use the same code but replace in the column definition ‘TitleTemplate’ by ‘CellContentTemplate’ (both can be used).

    Imported from legacy forums. Posted by Bernard (had 536 views)

    Xceed Support
    Member
    Post count: 5658

    Thanks for sharing! I am sure that this will help many clients who are trying to accomplish the same thing 🙂

    Imported from legacy forums. Posted by Jenny [Xceed] (had 3021 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you, I found this very useful.  In my case, I added the DataTemplate programmatically:

    dgc.Columns[“MyColumn”].TitleTemplate = (DataTemplate)FindResource(“myTitleTemplate”);

    Imported from legacy forums. Posted by Neo (had 691 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.