Silverlight – Converting text / Type to an object

28 02 2010

Problem:

Let’s say you have a series of ChildWindows that each show some data. e.g Add/Edit Customer, Add/Edit Vendor and etc.

Each ChildWindow has a name e.g. AddCustomer, AddVendor.

And you also have a series of Hyperlinks that are used to show these childwindows. Suppose you want to do is to have “one” method that all these Hyperlinks call to open their related childwindow. Each Hyperlink tag is holding the same name of the ChildWindow, i.e. AddCustomer, AddVendor as above.

Solution:

You can use Activator.CreateInstance which create object from type

    string childWindowTypeName = "ChildWindow1";//hyperlink.Tag.ToString()
    string nameSpace = this.GetType().Namespace;

    Type type = Type.GetType(string.Format("{0}.{1}", nameSpace, childWindowTypeName));

    ChildWindow c = Activator.CreateInstance(type) as ChildWindow;
    c.Show();




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);
        }




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
            }
        }