Bind a textblock inside the Expander

2 posts, 0 answers
  1. raghul
    raghul avatar
    11 posts
    Member since:
    Jul 2010

    Posted 20 Nov 2011 Link to this post

    Hi,

    I have got a scenario where I need to have a textblock inside the expander, Now when I try to bind the items, I am not able to view any items. I could not figure out what the problem is. Please find the codings which I have added.

    <StackPanel x:Name="OuterStack" DataContext="{Binding}">
                <Grid x:Name="OuterGrid">
                    <StackPanel x:Name="InnerStackPanel" Width="600" Height="450" >
                        <ListBox x:Name="lstBox">
                            <ListBoxItem AllowDrop="True" Height="50">
                                <StackPanel Orientation="Horizontal" x:Name="lstboxStackPanel">
                                    <telerik:RadExpander Width="360">
                                        <StackPanel x:Name="SplInsideExpander" Orientation="Horizontal">
                                            <TextBlock x:Name="TxtEmpAddress" Text="{Binding EmpAddress}"></TextBlock>
                                            <TextBlock x:Name="TxtEmpCity" Text="{Binding EmpCity}"></TextBlock>
                                        </StackPanel>
                                    </telerik:RadExpander>
                                    <TextBlock x:Name="TxtEmpName" Text="{Binding EmpName}" xml:space="preserve"></TextBlock>
                                    <TextBlock x:Name="TxtEmpid" Text="{Binding EmpId}"></TextBlock>
                                </StackPanel>
                            </ListBoxItem>
                        </ListBox>
                    </StackPanel>
                </Grid>
            </StackPanel>

    In the Code behind I have the following code
    public void BindEmpDetails()
            {
                 
                List<EmployeeDetails> lste = new List<EmployeeDetails>();
                for (int i = 0; i < 10; i++)
                {
                    EmployeeDetails obj = new EmployeeDetails();
                    obj.EmpId = i.ToString();
                    obj.EmpName = "Emp Name" + i;
                    obj.EmpCity = "City " + i;
                    obj.EmpAddress = "Address " + i;
                    lste.Add(obj);
                }
     
                OuterStack.DataContext = lste;
            }
     
    public class EmployeeDetails
        {
            public string EmpId { get; set; }
            public string EmpName { get; set; }
            public string EmpAddress { get; set; }
            public string EmpCity { get; set; }
     
            
        }

    Please let me know where I am going wrong? thx in advance
  2. raghul
    raghul avatar
    11 posts
    Member since:
    Jul 2010

    Posted 21 Nov 2011 Link to this post

    Hi all,

    I figured out what the problem is. Instead of finding the control inside the list box we could have that as
    <ListBox x:Name="lstBox" Width="1200" Height="400"
                                VerticalAlignment="Top" ItemsSource="{Binding}">
                           <ListBox.ItemTemplate>
                               <DataTemplate>
                                   <StackPanel Orientation="Horizontal" x:Name="lstboxStackPanel">
                                       <telerik:RadExpander Width="1200">
                                           <telerik:RadExpander.Header>
                                               <StackPanel x:Name="TestRadExpanderHeader" Orientation="Horizontal">
                                               <TextBlock x:Name="TxtEmpName" Text="{Binding EmpName}" xml:space="preserve" Width="Auto"></TextBlock>
                                               <TextBlock x:Name="TxtEmpid" Text="{Binding EmpId}" Width="Auto" ></TextBlock>
                                               </StackPanel>
                                           </telerik:RadExpander.Header>
                                               <Grid x:Name="Grd1">
                                               <Grid.RowDefinitions>
                                                   <RowDefinition Height="*">
                                                   </RowDefinition>
                                               </Grid.RowDefinitions>
                                               <Grid.ColumnDefinitions>
                                                   <ColumnDefinition Width="Auto"></ColumnDefinition>
                                                   <ColumnDefinition Width="Auto"></ColumnDefinition>
                                               </Grid.ColumnDefinitions>
                                            
                                           <StackPanel x:Name="SplInsideExpander" Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
                                               <TextBlock x:Name="TxtEmpAddress" Text="{Binding EmpAddress}" Width="1200" TextWrapping="Wrap" ></TextBlock>
                                           </StackPanel>
                                               <StackPanel x:Name="SplInsideExpander1" Orientation="Horizontal" Grid.Row="0" Grid.Column="2">
                                           <!--<TextBlock x:Name="TxtEmpCity" Text="{Binding EmpCity}" Width="Auto" TextWrapping="Wrap"></TextBlock>-->
                                           </StackPanel>
                                           </Grid>
                                       </telerik:RadExpander>
                                        
                                   </StackPanel>
                               </DataTemplate>
                           </ListBox.ItemTemplate>
                       </ListBox>
    and in the code behind we could have it as
    public List<MyEmployeeDetails> lstMyEmployeeDetails = new List<MyEmployeeDetails>();
     
            public void BindToExpander()
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                for (int i = 0; i < 10; i++)
                {
                    sb.Append("Employee Address Employee Address Employee Address Employee Address Employee Address Employee Address Employee Address Employee Address Employee Address Employee Address");
                    sb.Append("Employee Address Employee Address Employee Address Employee Address Employee Address Employee Address" + i);
                    MyEmployeeDetails objMyEmployeeDetails = new MyEmployeeDetails();
                    objMyEmployeeDetails.EmpId = i;
                    objMyEmployeeDetails.EmpName = "Employee Name " + i;
                    objMyEmployeeDetails.EmpAddress = sb.ToString();
                                                         
                    objMyEmployeeDetails.EmpCity = "Employee City Employee City Employee City Employee City Employee City Employee City" + i;
                    lstMyEmployeeDetails.Add(objMyEmployeeDetails);
                }
     
                lstBox.ItemsSource = lstMyEmployeeDetails;
            }
  3. DevCraft banner
Back to Top