Sharker Khaleed Mahmud Silverlight Tips & Tricks

January 20, 2010

Tricks and Tips 15 : Apply border in Silverlight DataGridRow Cell

This tip is an example of applying border in datagridrow cell


MainPage.xaml

<UserControl xmlns:my=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”  x:Class=”Tips.MainPage”
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:d=”http://schemas.microsoft.com/expression/blend/2008
    xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006
    mc:Ignorable=”d”
    d:DesignHeight=”300″ d:DesignWidth=”400″>
    <Grid x:Name=”LayoutRoot”>
        <my:DataGrid x:Name=”dglist” AutoGenerateColumns=”False”>
            <my:DataGrid.Columns>
                <my:DataGridTextColumn Binding=”{Binding Id}” Header=”Id” />
                <my:DataGridTemplateColumn Header=”Title”>
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid Height=”30″>
                                <Border BorderThickness=”1″ BorderBrush=”Black”>
                                    <TextBlock Text=”{Binding Title}” />   
                                </Border>
                            </Grid>
                       </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
            </my:DataGrid.Columns>
        </my:DataGrid>
    </Grid>
</UserControl>

MainPage.xaml.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;

namespace Tips
{
    public class ListItem
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
       
    }
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            List<ListItem> items = new List<ListItem>();
            items.Add(new ListItem { Id = “1″, Title = “Sample 1″});
            items.Add(new ListItem { Id = “2″, Title = “Sample 2″ });
            items.Add(new ListItem { Id = “3″, Title = “Sample 3″ });
            dglist.ItemsSource = items;
        }
    }
}

Sharker Khaleed Mahmud
Software Developer
(MCP,MCTS,MCPD[web])

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.