This is a migrated thread and some comments may be shown as answers.

Bind a textblock inside the Expander

1 Answer 35 Views
Expander
This is a migrated thread and some comments may be shown as answers.
raghul
Top achievements
Rank 1
raghul asked on 20 Nov 2011, 02:32 PM

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

1 Answer, 1 is accepted

Sort by
0
raghul
Top achievements
Rank 1
answered on 21 Nov 2011, 12:39 PM
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;
        }
Tags
Expander
Asked by
raghul
Top achievements
Rank 1
Answers by
raghul
Top achievements
Rank 1
Share this question
or