kalu singh rao

kalu singh rao

  • 289
  • 6.4k
  • 108.7k

WPF DataContext binding with ListBox

May 8 2014 8:10 AM
I want to bind list in listbox using datacontext but it is not working.
 
 XAML code is below
 

<Window x:Class="Examples.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="350" Width="525">

<Window.Resources>

<DataTemplate x:Key="data">

<TextBlock Name="txt" Text="{Binding Path=Name}"></TextBlock>

</DataTemplate>

</Window.Resources>

<Grid>

<ListBox Name="lbox" ItemTemplate="{DynamicResource data}" ItemsSource="{Binding det}"></ListBox>

</Grid>

</Window>

C# code is here
 
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

namespace Examples

{

/// <summary>

/// Interaction logic for MainWindow.xaml

/// </summary>

///

public class Detail

{

public string Name { get; set; }

public List<string> Loan = new List<string>();

public Detail()

{

Loan.Add(Name = "Angular");

Loan.Add(Name = "Backbone"); 
 
Loan.Add(Name = "Kinetic"); 

}

}

public partial class MainWindow : Window

{

Detail dt = new Detail();

public MainWindow()

{

InitializeComponent();

List<string> det = new List<string>();

det = dt.Loan;

this.DataContext = det;

lbox.ItemsSource = det;

}

}

}

 Please tell me which code is wrong and update working code.
Thank you in advanced .
 

Answers (3)