This is a migrated thread and some comments may be shown as answers.

GridViewDataColumn DataTemplate with DataType is not working

3 Answers 735 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rene
Top achievements
Rank 1
Rene asked on 07 Mar 2014, 11:12 AM
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;
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 10 Mar 2014, 04:03 PM
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.

0
Tom
Top achievements
Rank 1
answered on 05 Feb 2017, 11:44 PM

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?

0
Yoan
Telerik team
answered on 08 Feb 2017, 04:47 PM
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 you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Rene
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Tom
Top achievements
Rank 1
Yoan
Telerik team
Share this question
or