Template column in datagrid silverlight

Template column in datagrid silverlight:

In silverlight datagrid you can use any control inside the datagrid using this template column.
And also you can use all the events of the controls which you used as a template column(ie you can create MouseLeftbuttonDown, MouseLeftButtonUp etc...)

Output:


xaml code:

 <sdk:DataGrid AutoGenerateColumns="False" Height="141"  Margin="10,10,0,0" Name="dataGrid1"  Width="210" >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTemplateColumn Width="200" Header="User ID"  CanUserReorder="True" CanUserResize="True" CanUserSort="True" >
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock x:Name="textBox1" Text="{Binding UserID}" TextWrapping="Wrap" ToolTipService.ToolTip="ID Of the User"  Foreground="#FF0071BD"   VerticalAlignment="Center"/>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>   
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

If you want to use DataGridTextColumn:

Use the follwing code instead of  <sdk:DataGridTemplateColumn
<sdk:DataGridTextColumn Width="120"  Header="User ID" Binding="{Binding UserID}" >
                    <sdk:DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock"  >
                            <Setter Property="TextWrapping" Value="Wrap"/>
                        </Style>
                    </sdk:DataGridTextColumn.ElementStyle>

                </sdk:DataGridTextColumn>

C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace samp
{
    public partial class MainPage : UserControl
    {
          public MainPage()
          {
              InitializeComponent();

              loaddata();
          }

          private void loaddata()
          {
              List<Data> lobj = new List<Data>();
              lobj.Add(new Data() {UserID="0001"});
              lobj.Add(new Data() { UserID = "0001" });
              lobj.Add(new Data() { UserID = "0001" });
              lobj.Add(new Data() { UserID = "0001" });
              dataGrid1.ItemsSource = lobj;
          }
    }

    public class Data
    {
        public string UserID { get; set; }
    }

}

Comments

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

Add content type to SharePoint List/Library using REST API

A type named 'SP.Data. could not be resolved by the model error