GridViewDataColumn DataTemplate with DataType is not working

4 posts, 0 answers
  1. Rene
    Rene avatar
    8 posts
    Member since:
    Jan 2014

    Posted 07 Mar 2014 Link to this post

    Hey guys,
    I've got a problem which is really frustrating. I am trying to display content based on its type in a RadGridView. Therefore I am using a DataTemplate referenced to a DataType. Sadly it seems like the DataType isnt beeing recognized at all. If I change the DataMemberBinding from "{Binding}" to a member of the object which is of type string - it is working but thats not what I need, i need it to be working with my own Data types(classes). 

    Code:

    XAML
    <Window x:Class="RadControlsWpfApp1.MainWindow"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                    xmlns:local="clr-namespace:RadControlsWpfApp1"
                    xmlns:System="clr-namespace:System;assembly=mscorlib"
                    Title="MainWindow" Height="350" Width="525">
            <Grid>
            <telerik:RadGridView x:Name="Grid" Grid.Column="1" Margin="11,10,9,50" Grid.Row="1" ItemsSource="{Binding}" >
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn IsReadOnly="True" Header="test" DataMemberBinding="{Binding}" >
                        <telerik:GridViewDataColumn.Resources>
                            <DataTemplate DataType="{x:Type local:Foo}">
                                <TextBlock Text="test" />
                            </DataTemplate>
                        </telerik:GridViewDataColumn.Resources>
                    </telerik:GridViewDataColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
     
        </Grid>
    </Window>

    CS:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    namespace RadControlsWpfApp1
    {
        public class Foo
        {       
            public string Name { get; set; }
            public string Value { get; set; }
        }
        public class Foo2
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }
     
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        ///
        public partial class MainWindow : Window
        {
             
            public MainWindow()
            {
                var list = new List<object> { new Foo2() { Name = "Brian2", Value = "val2" }, new Foo() { Name = "Brian", Value = "val1" } };
     
     
                InitializeComponent();
                Grid.DataContext = list;
            }
        }
    }
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 10 Mar 2014 Link to this post

    Hello,

    Actually this is how the Binding works. As you set DataMemberBinding="{Binding}", 
    the .ToString() method of the bound item (which is of type Foo2 or Foo) will be invoked and the result will be displayed in the cell.

    If you would like to work with your full item, I can suggest you to use a Converter for the Binding and have the original item when the converter is evaluated. Another option would be to override the ToSting() methods of your classes and return the string you would like to have displayed.

    Regards,
    Didie
    Telerik

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

  3. Tom
    Tom avatar
    5 posts
    Member since:
    May 2016

    Posted 05 Feb in reply to Dimitrina Link to this post

    Why is this so hard!

    It is so damn frustrating, I have the same column of type MyType all over the place and I cannot get the column to apply the DataTemplate I have defined in another resources file, it should be an option in the RadGrid like ColumnDataTemplatesMode="DataType". Or there should be a telerik DataTemplateGridViewColumn that still accepts DataMemberBinding but attempts to resolve the DataTemplate instead of using ToString. I really can see no reason for you to make this so awkward and require devs to create hundreds of wrapper DataTemplates?

  4. Yoan
    Admin
    Yoan avatar
    1110 posts

    Posted 08 Feb Link to this post

    Hello,

    For such scenario, we had created our CellTemplateSelector. You can easily get the dataType of the column like so:
    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
           {
               var cell = container as GridViewCell;
               if (cell != null)
               {
                   var columnDataType = (cell.Column as GridViewBoundColumnBase).DataType;
               }


    Regards,
    Yoan
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top