none
tabbing functionality in silverlight app

    Question

  • Hi all,

    is it possible to have tabbing fucntionality in silverlight application?

    I mean similar to Internet explorer closebale tabs.

     

    Thank you,

    Pradeep

    Friday, May 04, 2012 12:35 AM

Answers

  • Hi Pradeep,

      You are opening new tabs  by clicking on the grid rows only, right ?  Because the above code is working perfectly for me.  

    The list<int>  mylist i mentioned earlier,   should be outside the methods. It belongs to the class , so dont write it inside the datagrid event handler.    You need to do some debugging i guess.  For example , check the value of the list after you have created a tab. It should contain the selectedIndex .  

    As for the bold lines ,  

     Well your requirement was that multiple instances of tab cannot be opened.   I also assumed that if you close the tab , you should be able to open that tab again from the grid.   If this is not what you wanted, just remove those 2 lines.

    This is my working code :

      List<int> val = new List<int>();
            void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                List<int> it = (List<int>)dataGrid1.SelectedItem;
    
                if (!val.Contains(dataGrid1.SelectedIndex))
                {
                    val.Add(dataGrid1.SelectedIndex);
                    TabItem t = new TabItem();
                    t.Header = "" + it[0];
                    t.MouseEnter += new MouseEventHandler(t_MouseEnter);
                    t.MouseLeave += new MouseEventHandler(t_MouseLeave);
                    t.HeaderTemplate = Resources["MyDataTemplate"] as DataTemplate;
                    FormatPage f = new FormatPage( dataGrid1.SelectedIndex, "" + it[0], "" + it[1], "" + it[2], "" + it[3], "" + it[4]);
                    t.Content = f;
                    tabControl1.Items.Add(t);
                }
            }

     Did it on the fly just now, and its working. Please try .

    Hope it Helps Laughing

    Wednesday, May 09, 2012 4:30 AM

All replies

  • Hi Pradi,

        You can have tabbing functionality. Please see this link .

    http://msdn.microsoft.com/en-us/library/system.windows.controls.tabcontrol(v=VS.95).aspx

    You have code samples at the bottom that should make everything clear for you.

    Hope it Helps Laughing

    Friday, May 04, 2012 1:12 AM
  • Thank you Phani Kiran challa,

    I've gone through that article. They've created tabs in design.

    But i'm trying to add tabs dynamically, i mean when you click data on certain page it should open a closeble new tab with that data.

    Thanks,

    Pradi

    Friday, May 04, 2012 1:55 AM
  • Hello,

    you can dynamically add tabs to tabcontrol from code at click event

    private void myData_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
    TabItem ti = new TabItem(); ti.Header = "First TabItem!"; ti.Content = "This is the first TabItem's content."; myTabControl.Items.Add(ti);
    }

    Friday, May 04, 2012 3:01 AM
  • Hi Nikituki,

    I've written the same thing in click event

     private void CaseButton_Click(object sender, RoutedEventArgs e)
            {
    
                 if (tabItem1.IsSelected)
                  {
                     TabItem tabItem = new TabItem();
                      tabItem.Header = "BOP 895658";
                     
                      TabControl1.Items.Add(tabItem);
            }

     

    It's opening an empty tab(without close (x) button) and i'm not getting how to get data on it.

     

    tabItem.content = "Some Data";

    i've tried this previously But its not working.

    I'm a fresh graduate and new to silverlight.

     

    Thank You,

    Pradi

     

    Friday, May 04, 2012 3:50 AM
  • Hi,

       I implemented the functionality.  I used a header template . Just see if you can tweak it to change button position, shape etc.  Here is the code.   I made it such that the first tab cannot be closed  as i created it in xaml.  You can make everything close by adding all the tabs through code  dynamically.

    Xaml :

     

    <UserControl x:Class="Headers.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" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    
        <UserControl.Resources>
            
                <DataTemplate x:Key="MyDataTemplate">
                    <Grid>
                        <TextBlock  Width="40" Height="20" Text="{Binding}" ></TextBlock>
                        <Button x:Name="btn" Click="btn_Click" Content="X" Width="20" Height="20" Margin="40,0,0,0"></Button>
                    </Grid>
                </DataTemplate>
            
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <sdk:TabControl Height="170" HorizontalAlignment="Left" Margin="45,71,0,0" Name="tabControl1" VerticalAlignment="Top" Width="307">
    
                <sdk:TabItem  Name="tabItem1" Header="hello" HeaderTemplate="{StaticResource MyDataTemplate}">
                    <Grid>
                        <Button Content="Create new tab" Height="23" HorizontalAlignment="Left" Margin="26,34,0,0" Name="button1" VerticalAlignment="Top" Width="112" Click="button1_Click" />
                    </Grid>
                </sdk:TabItem>
                
            </sdk:TabControl>
        </Grid>
    </UserControl>
    

     

     

    Xaml.cs:

    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;
    
    namespace Headers
    {
        public partial class MainPage : UserControl
        {
            TabItem temptab;
            public MainPage()
            {
                InitializeComponent();
               
                 
            }
    
            void t_MouseLeave(object sender, MouseEventArgs e)
            {
    
                //Grid g = (Grid)temptab.Header;
                //Button b = (Button)g.Children[1];
                
                
                
                temptab = null;
            }
    
            void b_MouseEnter(object sender, MouseEventArgs e)
            {
                
            }
    
            void t_MouseEnter(object sender, MouseEventArgs e)
            {
                temptab = (TabItem)sender;
            }
    
            private void btn_Click(object sender, RoutedEventArgs e)
            {
                if (temptab != null)
                {
                    tabControl1.Items.Remove(temptab);
                   
                }
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                TabItem t = new TabItem();
                t.Header = "New tab";
                t.MouseEnter += new MouseEventHandler(t_MouseEnter);
                t.MouseLeave += new MouseEventHandler(t_MouseLeave);
                t.HeaderTemplate = Resources["MyDataTemplate"] as DataTemplate;
                tabControl1.Items.Add(t);
            }
        }
    }
    

     Hope it Helps Laughing

    Friday, May 04, 2012 4:23 AM
  • Thank You Phani Kiran Challa.

    Can we get data on those newly created tabs dynamically?

    I've 5 rows in grid and first column(which identifies each rows is some number ) is hyperlink.

    So now when i click those values in first column empty tabs are being open. i need load the entire row data on those respective tabs. For this i made one page with some format  for data and tried attching it to tab but its not getting attached.

    Is this right way to put data on tabs or there is another way? 

    Thanks,

    Pradi

    Friday, May 04, 2012 6:58 AM
  • Hi Pradi,

        I saw your requirement. But i dont know whtether i understood it correctly or not.  

    You have a separate Datagrid, with some data. Only the first link is hyperlink, which should open the whole row data in a new tab.   You have created a page with formatting, that displays the data in the newly created tab .   But when you are creating the new tab, you are not getting any content . 

     Is that the issue now ?

     

    Friday, May 04, 2012 7:23 AM
  • Hi ,

    If it is, then this should solve it.  I took some dummy data .  Add this code  to your mainpage.xaml.cs

      public MainPage()
            {
                InitializeComponent();
                List<List<int>> temp = new List<List<int>>();
    
                List<int> t1 = new List<int>() { 11, 12, 13, 14, 15 };
                List<int> t2 = new List<int>() { 21, 22, 23, 24, 25 };
                List<int> t3 = new List<int>() { 31, 32, 33, 34, 35 };
                List<int> t4 = new List<int>() { 41, 42, 43, 44, 45 };
                List<int> t5 = new List<int>() { 51, 52, 53, 54, 55 };
    
                temp.Add(t1);
                temp.Add(t2);
                temp.Add(t3);
                temp.Add(t4);
                temp.Add(t5);
    
                dataGrid1.AutoGenerateColumns = true;
                dataGrid1.ItemsSource = temp;
                dataGrid1.SelectionChanged += new SelectionChangedEventHandler(dataGrid1_SelectionChanged);
                 
            }
    
            void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                List<int> it = (List<int>)dataGrid1.SelectedItem;
    
                TabItem t = new TabItem();
                t.Header = ""+ it[0];
                t.MouseEnter += new MouseEventHandler(t_MouseEnter);
                t.MouseLeave += new MouseEventHandler(t_MouseLeave);
                t.HeaderTemplate = Resources["MyDataTemplate"] as DataTemplate;
                FormatPage f = new FormatPage(""+it[0],""+it[1],""+it[2],""+it[3],""+it[4]);
                t.Content = f;
                tabControl1.Items.Add(t);
    
            }

    In your mainpage.xaml ,  add

      <sdk:DataGrid AutoGenerateColumns="False" Height="132" HorizontalAlignment="Left" Margin="40,93,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="214" />

    Then add a new page FormatPage.xaml

    <UserControl x:Class="Headers.FormatPage"
        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" Background="White">
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,41,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,70,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,99,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,128,0,0" Name="textBox5" VerticalAlignment="Top" Width="120" />
        </Grid>
    </UserControl>
    

    In the FormatPage.xaml.cs , add a constructor

      public FormatPage(string s1,string s2,string s3,string s4,string s5)
            {
                InitializeComponent();
    
                textBox1.Text = s1;
                textBox2.Text = s2;
                textBox3.Text = s3;
                textBox4.Text = s4;
                textBox5.Text = s5;
    
    
            }

     Now your Tabs should contain the data .

    Hope it Helps Laughing 

    Friday, May 04, 2012 7:39 AM
  • Thanks a lot for quick reply Smile.

    I'll try this now, hope it works.

    Is this the only way to get data on dynamically created tabs?

     

    Friday, May 04, 2012 8:09 AM
  • hi,

     As you know, TabItem has content property. Anything like a canvas or grid can also be used, if you dont want to use a new page.  But to create the canvas /grid structure with your items or write the code dynamically, it is easier to use a new Silverlight page/user control.

    Of course, you could always use a DataTemplate  , and add a canvas/grid to the tabitem.content with this datatemplate. You could use bindings too , but i thought the first way was easy to implement anyway

    Hope it Helps Laughing

    Friday, May 04, 2012 10:25 AM
  • Hi Phani Kiran Challa,

    I tried with dummy data which u posted earlier for displaying data in tab, but i'm getting errors as like InitializeComponent() and dataGrid1 doesn't exist in current context. I added all the things which u posted.

     

    Monday, May 07, 2012 7:26 AM
  • Hi,

    My Project namespace is Headers. In case your project name is different, change the name in the first line of Mainpage.xaml.   

    <UserControl x:Class="Headers.MainPage" 
       
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

    Change that Headers.MainPage to "Your Project Name".MainPage

    MainPage()  is a constructor.  Change that name to the name of your UserControl or Page incase you changed it .  InitializeComponent () will automatically be recognized.  

    dataGrid1 is the name of my datagrid.  Did you Drag a datagrid in your designer ? If you have done, please name it to dataGrid1 .   

     Hope it Helps Laughing

    Monday, May 07, 2012 7:32 AM
  • Hi,

    yeah changed, now no errors but after running,

    browser is not showing anything. Only some silverlight dots rotate and go

    Monday, May 07, 2012 7:58 AM
  • Hi, Even in the mainpage.xaml.cs, did you change the namespace ? It might be because of that. Hope. It helps :)
    Monday, May 07, 2012 8:08 AM
  • Hi,

    I got it. I didnt remove previous "Create new tab" from mainpage.xaml button and changed its click to 

    dataGrid1_SelectionChanged() insted of button_click().

    Now i removed the whole button line so its coming.

    There some numbers

    Capacity item Count

    8                      5

    8                      5

    8                       5

    when i click on 8 i'm getting a new tab with textbox containg 11,12..21,22...

    This is the same thing i want but i want whole row as data in new tab when i click  first column of that row.

    what i understood from this is that  this data

    8          5

    has nothing to do wit the data that comes  in new tab.?

    Am i right? :) 

     

    Monday, May 07, 2012 8:34 AM
  • Hi, Exactly. That was just a sample. I wrote it just for filling the grid so that the inner data can be copied to the new tab.

    Your your data as Itemsource for the datagrid, instead of the List<int> objects i used.  Those 8  5  are coming because of that.   If you use proper data , you will get the correct display in the data grid also.

    ( see this for more info ..  http://xamlandmore.blogspot.in/2012/04/bindings-in-silverlight-binding-user.html )

    I hope the code solved your issue :)

    Monday, May 07, 2012 9:05 AM
  • Hi,

    one more question!!

    Currently in the first tabItem(which is bold letters) i'm using grid and populating it with values from database tables. This is done using edmx file.

    <UserControl x:Class="LastApplication.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="500" d:DesignWidth="900" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
        xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
        xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation">
    
        <UserControl.Resources>
            <DataTemplate x:Key="MyDatatemplate">
                <Grid>
                    <TextBlock Width="40" Height="20" Text="{Binding}" ></TextBlock>
                    <Button x:Name="btn" Click="btn_Click" Content="X" Width="20" Height="20" Margin="40,0,0,0"></Button>              
                </Grid>            
            </DataTemplate>
        </UserControl.Resources>
    
        <Grid x:Name="LayoutRoot" Background="White">
            <!--<sdk:Frame Name="NewFrame" Margin="12,12,93,48">-->
                <sdk:TabControl Height="400" HorizontalAlignment="Left" Margin="12,12,0,0" Name="TabControl1" VerticalAlignment="Top" Width="888">
             
                    <sdk:TabItem Name="tabItem1">
     <sdk:TabItem.Header> 
     <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="Tab Item1" Margin="1,1,1,1" VerticalAlignment="Center"/>
     <Button Content="X" Click="btn_Click" />
     </StackPanel>
     </sdk:TabItem.Header> 
     <Grid Height="367" Width="753"> 
     <Grid.RowDefinitions>
     <RowDefinition Height="172*" /> 
     <RowDefinition Height="195*" /> 
     </Grid.RowDefinitions>
     <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="64*" />
     <ColumnDefinition Width="0*" />
     <ColumnDefinition Width="779*" /> 
     </Grid.ColumnDefinitions> 
     <sdk:DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="704" Margin="15,20,0,0" Grid.ColumnSpan="3" Grid.RowSpan="2">
    <sdk:DataGrid.Columns> 
    <sdk:DataGridTemplateColumn x:Name="templatecolumn" Header="Task Number" Width="Auto" IsReadOnly="True" CanUserReorder="True" CanUserResize="True" CanUserSort="True">
     <sdk:DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
     <StackPanel VerticalAlignment="Center" >
     <HyperlinkButton x:Name="TaskNoButton" IsTabStop="False" Content="{Binding Path=Reference_NR}" Width="Auto" Tag="TaskNumber" Click="CaseButton_Click" Foreground="MidnightBlue" FontWeight="SemiBold" TargetName="_blank"/> 
     </StackPanel> 
     </DataTemplate>
     </sdk:DataGridTemplateColumn.CellTemplate> 
     <sdk:DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
     <HyperlinkButton x:Name="TaskNoButton" IsTabStop="False" Content="{Binding Path=Reference_NR}" Width="Auto" Foreground="MidnightBlue" FontWeight="SemiBold"/>
     </DataTemplate> 
     </sdk:DataGridTemplateColumn.CellEditingTemplate>
     </sdk:DataGridTemplateColumn> 
    <sdk:DataGridTextColumn Binding="{Binding Path=Agency_NM}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Agency Details" Width="Auto" /> 
    <sdk:DataGridTextColumn Binding="{Binding Path=Status_DESC}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Status" Width="Auto" />
     <sdk:DataGridTextColumn Binding="{Binding Path=Type_DESC}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Transaction" Width="Auto" /> 
    <sdk:DataGridTextColumn Binding="{Binding Path=TaskDue_DT}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Due Date" Width="Auto" /> <sdk:DataGridTextColumn Binding="{Binding Path=TaskEnd_DT}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Expiry Date" Width="Auto" />
     <sdk:DataGridTextColumn Binding="{Binding Path=UserName}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="User" Width="Auto" /> <sdk:DataGridTextColumn Binding="{Binding Path=Agency_Details_ID}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Agency Code" Width="Auto" /> 
    <sdk:DataGridTextColumn Binding="{Binding Path=Priority_CD}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Priority" Width="Auto" /> 
    </sdk:DataGrid.Columns> 
    </sdk:DataGrid> 
    </Grid> 
    </sdk:TabItem> 
     </sdk:TabControl>
     </Grid>
     </UserControl>

     

    1. But now when click this data i'm getting new tabitem(new tab).  so to display data according this method i need to create format page and bind it to tabitem. Then can i use same format page for mutiple tabs not just one tab or two?

    2 . Here(means new tab) i want data to be displayed like form data not like grid in base tabitem. is it possible?

    3. if it is possible then how to display form data(i.e in  text boxes) means how get data in to text boxes, and from 

     where database tables or from the grid which is used in first tabitem(bold letters)?

    I really dont know how to do this. It might be a silly question :)

    please if you know then tell me.

     

    Thank You, i learnt some new things from You:)

    Pradeep

     

     

     

     

     

    Monday, May 07, 2012 10:39 AM
  • Hi Pradeep,

        Yes, that same Format page can be used for multiple tabs.  You can customize the appearance of the Format page in your designer as you want.  

    See the FormatPage.xaml i posted earlier  . It uses text boxes  instead of Grid.   

    Currently i passed data through the constructor , (but alternatively you can bind the data to the textboxes through code.)

    void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
           
    {
               
    List<int> it = (List<int>)dataGrid1.SelectedItem;

               
    TabItem t = new TabItem();
                t
    .Header = ""+ it[0];
                t
    .MouseEnter += new MouseEventHandler(t_MouseEnter);
                t
    .MouseLeave += new MouseEventHandler(t_MouseLeave);
                t
    .HeaderTemplate = Resources["MyDataTemplate"] as DataTemplate;
               
    FormatPage f = new FormatPage(""+it[0],""+it[1],""+it[2],""+it[3],""+it[4]);
                t
    .Content = f;
                tabControl1
    .Items.Add(t);

           
    }

    This was how i did it .    I took data from the selected row in the datagrid and since it was of type List<int> , i sent the list items as parameters to the constructor. 

    Hope it Helps Laughing

    Monday, May 07, 2012 9:29 PM
  • Hi,

    i'm currently trying this format page in my app.

    But i'm getting exception at this line(bold)

                    List<CaseMgm> cm = (List<CaseMgm>)dataGrid1.SelectedItem;                 
                    TabItem tabItem = new TabItem();
                    tabItem.Header = "" + cm[0] ;
                    tabItem.HeaderTemplate = Resources["MyDatatemplate"] as DataTemplate;
                    tabItem.MouseEnter += new MouseEventHandler(t_MouseEnter);
                    tabItem.MouseLeave += new MouseEventHandler(t_MouseLeave);

    Exception details

     System.InvalidCastException was unhandled by user code
       Message=Unable to cast object of type 'CaseMgm' to type 'System.Collections.Generic.List`1'.
      StackTrace:
           at LastApplication.MainPage.CaseButton_Click(Object sender, RoutedEventArgs e)
           at System.Windows.Controls.Primitives.ButtonBase.OnClick()
           at System.Windows.Controls.HyperlinkButton.OnClick()
           at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
           at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
           at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
    

    i have written the above code in click event Casebutton_Click() which is attached to hyperlink on the first column of datagrid.

    In List<CaseMgm> CaseMgm is class which contains details which i need to bind to controls created in format page.

    Thank you,

    Pradi

     

    Tuesday, May 08, 2012 2:55 AM
  • hi, . Your exception shows that cm should be of type casemgm , not of list<casemgm>
    Tuesday, May 08, 2012 3:03 AM
  • okay. even if i use like this

    CaseMgm cm = (CaseMgm)dataGrid1.SelectedItem;

    it is giving same invalid cast exception. Do u know whats wrong with this?

    Tuesday, May 08, 2012 3:11 AM
  • Hi, what are you assigning to the datagrid itemsource ?  Can you post the code  ? 

    Also , to see how to bind user defined data , look at my post   http://xamlandmore.blogspot.in/2012/04/bindings-in-silverlight-binding-user.html

     

    Tuesday, May 08, 2012 3:34 AM
  • Here is the code, where datagrid1.Itemsource = abc; and abc is of type  List<CaseMgm>

     List<CaseMgm> abc = new List<CaseMgm>();
    
                        foreach (Case_Details cstat in query)
                        {
                            CaseMgm cs = new CaseMgm();
    
                            aspnet_Users asp = new aspnet_Users();
                            if (cstat != null)
                            {
                                cs.Reference_NR = cstat.Reference_NR;
                                cs.Agency_NM = cstat.Agency_Details.Agency_NM;
                                cs.Status_DESC = cstat.CaseStatus_MST.Status_DESC;
                                cs.Type_DESC = cstat.CaseType_MST.Type_DESC;
                                cs.TaskDue_DT = cstat.Work_Task.FirstOrDefault().TaskDue_DT.Value;
                                cs.TaskEnd_DT = cstat.Work_Task.FirstOrDefault().TaskEnd_DT;
                                cs.UserName = asp.UserName;
                                //cs.LOB_DESC = cstat.
                                cs.Agency_Details_ID = cstat.Agency_Details.Agency_Details_ID;
                                cs.Priority_CD = cstat.Priority_CD;
    
                                abc.Add(cs);
                            }
    
                            else
                            {
                                dataGrid1.ItemsSource = abc;
                            }
                           
                            dataGrid1.ItemsSource = abc;
                         
                        }

    And code where i'm trying to assign data from grid to local variable(of type CaseMgm)

     private void CaseButton_Click(object sender, RoutedEventArgs e)
            {
               
                if (tabItem1.IsSelected)
                 {
    
                    CaseMgm cm = (CaseMgm)dataGrid1.SelectedItem;
                    
                    TabItem tabItem = new TabItem();
                    tabItem.Header = "" + cm.Reference_NR ;
                    tabItem.HeaderTemplate = Resources["MyDatatemplate"] as DataTemplate;
                    tabItem.MouseEnter += new MouseEventHandler(t_MouseEnter);
                    tabItem.MouseLeave += new MouseEventHandler(t_MouseLeave);
     
    Tuesday, May 08, 2012 5:22 AM
  • Hi,

    Since your ItemSource is List<CaseMgm> ,  your selectedItem will be of type CaseMgm.  So this statement is correct.

    CaseMgm cm = (CaseMgm)dataGrid1.SelectedItem;


    But in your for loop, you are repeatedly setting the dataGrid.ItemSource to abc , and that is being set every time the loop is run . I think you dont need to write the else part at all . Also, you should put the

     

    dataGrid1.ItemsSource = abc;

    Outside the for loop and not within it.   Once try these changes and see if it helps .   Laughing

     

    Tuesday, May 08, 2012 6:36 AM
  • Hi,

    Now i'm not getting that exception. Its working and on new tab data is coming.

    I dont know how it got fixed, i didnt even make single change.

    Thanks a lot,  Phani Kiran Challa

    one more thing how to avoid repeatation of same tab?

     

    Pradi

    Tuesday, May 08, 2012 7:21 AM
  • Hi Pradeep,

    Here is the logic.  Take a   List<int> in the class .  When you click on the datagrid , add the selected index of the datagrid to this list.  Also , add that index to a  variable in the new tab .  

    When you are opening a new tab, check the list.  See If the index is there in the list and if it is , dont open the tab.  Also, when you are closing the tab, remove the index number from the list using the stored variable in the tab . 

    Hope it Helps Laughing

    Tuesday, May 08, 2012 8:20 AM
  • hi,

    i dont have the slightest idea how to use selectdIndex of Datagrid.

    Can give me an example or any blog where selectedidex used for this thing.

    I searched in google, after u posted this but didn't get any clear idea how to do this?

    Thank You

    Pradi

    Tuesday, May 08, 2012 10:03 AM
  • Hi,

    Let the List<int>  you will using be mylist . 

    List<int> mylist = new List<int>();

      

     

     
               
    List<int> it = (List<int>)dataGrid1.SelectedItem;

               
    TabItem t = new TabItem();
                t
    .Header = ""+ it[0];
                t
    .MouseEnter += new MouseEventHandler(t_MouseEnter);
                t
    .MouseLeave += new MouseEventHandler(t_MouseLeave);
                t
    .HeaderTemplate = Resources["MyDataTemplate"] as DataTemplate;
               
    FormatPage f = new FormatPage(""+it[0],""+it[1],""+it[2],""+it[3],""+it[4]);
                t
    .Content = f;
                tabControl1
    .Items.Add(t);

     

    In the above code,  just make 2 changes.

     Add    int indexvalue = dataGrid1.SelectedIndex;

              mylist.add(indexvalue);

     Change FormatPage constructor to take one more parameter , i.e index value

     =new FormatPage(indexvalue, ""+it[0],""+it[1],""+it[2],""+it[3],""+it[4]);

    In the FormatPage.xaml.cs, create a new variable ..   public int myindex ;

    in the updated constructor, set myindex to the first value .

    In thebtn_click event , you need to get back that selectedindex number

      private void btn_Click(object sender, RoutedEventArgs e)
           
    {
               
    if (temptab != null)
               
    {
    FormatPage f = (FormatPage)temptab.Content;
    mylist.remove (f.myindex);
                    tabControl1.Items.Remove(temptab);
                   
               
    }
           
    }

    Hope it helps  Laughing

    Tuesday, May 08, 2012 11:44 AM
  • hi,

    i've added all these line. But it's not working.

    when i click on row for two times two same tabs are opening.

    Once tab is opened for a row if i click same row again, it should not open new tab insted it should show

    the already opened tab. Is this possible?

     

     

    Add    int indexvalue = dataGrid1.SelectedIndex

    And in debugg mode this indexvalue in the above line is showing is 0 (zero).

    private void btn_Click(object sender, RoutedEventArgs e)
           
    {
               
    if (temptab != null)
               
    {
    FormatPage f = (FormatPage)temptab.Content;
    mylist.remove (f.myindex);
                    tabControl1.Items.Remove(temptab);
                   
               
    }
           
    }

    And i didnt understand this part. Why are we writing these below two lines(bold)

    FormatPage f = (FormatPage)temptab.Content;
    mylist.remove (f.myindex);

     in btn_Click()  . This btn_Click is called when we click on x(close) button on each tabs.

    Am i wrong somewhere?

     

     

    Pradi

    Wednesday, May 09, 2012 3:56 AM
  • Hi Pradeep,

      You are opening new tabs  by clicking on the grid rows only, right ?  Because the above code is working perfectly for me.  

    The list<int>  mylist i mentioned earlier,   should be outside the methods. It belongs to the class , so dont write it inside the datagrid event handler.    You need to do some debugging i guess.  For example , check the value of the list after you have created a tab. It should contain the selectedIndex .  

    As for the bold lines ,  

     Well your requirement was that multiple instances of tab cannot be opened.   I also assumed that if you close the tab , you should be able to open that tab again from the grid.   If this is not what you wanted, just remove those 2 lines.

    This is my working code :

      List<int> val = new List<int>();
            void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                List<int> it = (List<int>)dataGrid1.SelectedItem;
    
                if (!val.Contains(dataGrid1.SelectedIndex))
                {
                    val.Add(dataGrid1.SelectedIndex);
                    TabItem t = new TabItem();
                    t.Header = "" + it[0];
                    t.MouseEnter += new MouseEventHandler(t_MouseEnter);
                    t.MouseLeave += new MouseEventHandler(t_MouseLeave);
                    t.HeaderTemplate = Resources["MyDataTemplate"] as DataTemplate;
                    FormatPage f = new FormatPage( dataGrid1.SelectedIndex, "" + it[0], "" + it[1], "" + it[2], "" + it[3], "" + it[4]);
                    t.Content = f;
                    tabControl1.Items.Add(t);
                }
            }

     Did it on the fly just now, and its working. Please try .

    Hope it Helps Laughing

    Wednesday, May 09, 2012 4:30 AM
  • yeah i'm opening new tabs by clicking on the grid rows.  however i dont want to  open same tab again.

    similar to Visual studio, when you click on file if it is not opened it'll open in a new tab, if already opened it'll show the same tab.

    If it's working perfectly for you  then i'll debugg again to know where am i wrong?

     

    Pradi

    Wednesday, May 09, 2012 5:46 AM
  • Hi Phani Kiran Challa,

    It's working fine now.

    Thanks a lot for your help Smile

    PradiSmile

    Wednesday, May 09, 2012 8:14 AM