Answer the question
In order to leave comments, you need to log in
How to populate a ListBox with an ItemTemplate (WPF)?
Hello!
There is a ListBox:
<ListBox x:Name="myListBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="500" Margin="10,10,0,0" VerticalAlignment="Top" Width="420" ItemTemplate="{DynamicResource NewsDataTemplate}"/>
<TextBlock Text="{Binding Path=Title, FallbackValue=Title}" />
Answer the question
In order to leave comments, you need to log in
xaml, setting DataType="local:Line" is optional.
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate DataType="local:Line">
<TextBlock Text="{Binding Title}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public MainWindow()
{
InitializeComponent();
var lines = new List<Line>();
lines.Add(new Line() { Title = "1" });
lines.Add(new Line() { Title = "2" });
lines.Add(new Line() { Title = "3" });
this.DataContext = lines;
}
public class Line
{
public string Title { get; set; }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question