GridViewDataColumn DataTemplate with DataType is not working

2 posts, 0 answers
  1. Rene
    Rene avatar
    11 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). 



    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>


    Code behind:
    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

    Hi,

    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.

Back to Top