A TextBlock control in .NET 3.5 provides a lightweight control for displaying small amounts of flow content and A ListBox control hosts a collection of ListBoxItem.
We'll create and use these controls to understand clearly.
I have set of strings in my code behind(c# file , now i will bind each string to the Textblock control and then i will add these Textblock controls to the Listbox.
Means , the Textblocks here are Listboxitems.
XAML code for TextBlock creation :
<TextBlock x:Name="txtblkname" Height="40" Text="This is TextBlock."
Foreground="Red">
</TextBlock>
XAMLcode for ListBox creation:
<ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"
We'll create and use these controls to understand clearly.
I have set of strings in my code behind(c# file , now i will bind each string to the Textblock control and then i will add these Textblock controls to the Listbox.
Means , the Textblocks here are Listboxitems.
XAML code for TextBlock creation :
<TextBlock x:Name="txtblkname" Height="40" Text="This is TextBlock."
Foreground="Red">
</TextBlock>
XAMLcode for ListBox creation:
<ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="194" Height="200">
<ListBoxItem Content="Coffie"></ListBoxItem>
<ListBoxItem Content="Tea"></ListBoxItem>
<ListBoxItem Content="Orange Juice"></ListBoxItem>
<ListBoxItem Content="Milk"></ListBoxItem>
<ListBoxItem Content="Iced Tea"></ListBoxItem>
<ListBoxItem Content="Mango Shake"></ListBoxItem>
</ListBox
Now, i would like to create an implicit DataTemplate
that works on an array (or IEnumerable) of my class. This way i have a template
that describes how to render a bunch of items instead of just one.
<StackPanel Width="300" Height="300">
<ListBox x:Name="lisbx" Width="150" Height="150" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Width="300" Height="300">
<ListBox x:Name="lisbx" Width="150" Height="150" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Height="50" Width="100"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
Now i will create array of strings in my code behind file in the window loaded event because i have used window.We can bind the data in any event like button_click based on the requirement.
My Code Behind code looks like this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] names = {"Text","Text1","Text2","Text3","Text4"};
lisbx.ItemsSource = names;
}
My Code Behind code looks like this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] names = {"Text","Text1","Text2","Text3","Text4"};
lisbx.ItemsSource = names;
}
Here we have to bind these Text's to the textblock and add those to the listbox as listbox items.
My XAML code looks like this with Binding:
Here the textblock is created dynamically and the text is binded to it from code behind file in the window loaded event.
And these textblock is added to the listbox as a listboxitem.
My XAML code looks like this with Binding:
<StackPanel Width="300" Height="300">
<ListBox x:Name="lisbx" Width="150" Height="150" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Height="50" Width="100" Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
Here the textblock is created dynamically and the text is binded to it from code behind file in the window loaded event.
And these textblock is added to the listbox as a listboxitem.
This is about simple Data binding from code behind with example in WPF.