Adding DataGrid RowDetailsTemplate or TemplateColumn in Code Bhind

27 01 2010

Create a DataTemplate structure in resources.

Then we can  load the DataTemplete and set it to DataGrid rowDetailsTemplate /TemplateColumn

We can use a StackPanel/Grid inside the DataTemplate and set load event on StackPanel to add new children at runtime.

Example below:

XAML:

<UserControl.Resources>
<DataTemplate x:Key=“rowDT”>
<StackPanel Loaded=“StackPanel_Loaded” Orientation=“Horizontal” Background=“White”>

</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name=“LayoutRoot” Background=“Wheat”>
<data:DataGrid x:Name=“myDataGrid” AutoGenerateColumns=“False”>
</data:DataGrid>
</Grid>

Code Behind For RowDetailsTemplate:

        public Page()
        {
            InitializeComponent();

            var rowDetailsTemplate = (DataTemplate)this.Resources["rowDT"];
            myDataGrid.RowDetailsTemplate = rowDetailsTemplate;
            myDataGrid.ItemsSource = GridData.GetGridData();
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var templateChildPanel = sender as StackPanel;
            var detailTextBlock = new TextBlock
            {
                Width = 350,
                TextWrapping = TextWrapping.Wrap,
                Text = "I am Amyo Kabir"
            };

            //If you need binding
            //var dataBinding = new Binding("DetailsText");
            //dataBinding.Mode = BindingMode.TwoWay;
            //detailTextBlock.SetBinding(TextBlock.TextProperty, dataBinding);
            templateChildPanel.Children.Add(detailTextBlock);
        }

Code Behind For TemplateColumn:

        public Page()
        {
            InitializeComponent();

            var cellTemplate = (DataTemplate)this.Resources["rowDT"];

            var dataGridTemplateColumn = new DataGridTemplateColumn();
            dataGridTemplateColumn.Header = "Run Time Column";
            dataGridTemplateColumn.CellTemplate = cellTemplate;

            myDataGrid.Columns.Add(dataGridTemplateColumn);
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var templateChildPanel = sender as StackPanel;
            var detailTextBlock = new TextBlock
            {
                Width = 350,
                TextWrapping = TextWrapping.Wrap,
                Text = "I am Amyo Kabir"
            };

            //If you need binding
            //var dataBinding = new Binding("DetailsText");
            //dataBinding.Mode = BindingMode.TwoWay;
            //detailTextBlock.SetBinding(TextBlock.TextProperty, dataBinding);
            templateChildPanel.Children.Add(detailTextBlock);
        }
Advertisements




Configure different XAML page as start page using single XAP

1 01 2010

Suppose you have a Silverlight application with 2 page

1) AnimationPage1.xaml

2) AnimationPage2.xaml

And suppose in your Web project you have 2 page

1) TestPage1.aspx

2) TestPage2.aspx

Now you want to use same xap on both .aspx  page but with different animation (different xaml page)

Ok

You can use the initParams property of <object> tag that host Silverlight App:

TestPage1.aspx:

        <object data="data:application/x-silverlight-2,"
            type="application/x-silverlight-2"
            width="100%" height="100%">
            <param name="source" value="../ClientBin/Sample.xap" />
            <param name="initParams" value="startPage=AnimationPage1" />
            <param name="onError" value="onSilverlightError" />
            <param name="background" value="white" />
            <param name="minRuntimeVersion" value="3.0.40624.0" />
            <param name="autoUpgrade" value="true" />
            <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"
            style="text-decoration: none">
                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                    style="border-style: none" />
            </a>
        </object>

TestPage2.aspx:

        <object data="data:application/x-silverlight-2,"
            type="application/x-silverlight-2"
            width="100%" height="100%">
            <param name="source" value="../ClientBin/Sample.xap" />
            <param name="initParams" value="startPage=AnimationPage2" />
            <param name="onError" value="onSilverlightError" />
            <param name="background" value="white" />
            <param name="minRuntimeVersion" value="3.0.40624.0" />
            <param name="autoUpgrade" value="true" />
            <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"
            style="text-decoration: none">
                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                    style="border-style: none" />
            </a>
        </object>

Now in Silverlight application open the App.xaml

Modify the code like below:

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (e.InitParams.ContainsKey("startPage"))
            {
                if (e.InitParams["startPage"] == "AnimationPage1")
                {
                    this.RootVisual = new AnimationPage1();
                }
                else if (e.InitParams["startPage"] == "AnimationPage2")
                {
                    this.RootVisual = new AnimationPage2();
                }
                else
                {
                    // Assign any default page
                }
            }
            else
            {
                // Assign any default page
            }
        }