views:

30

answers:

2

I have a listbox and I am trying to Bind DataTable with it. I debug the Code and there is data in the DataTable but it does not show the data inside the DataList.

<Window.Resources>
    <local:myCurrencyColor x:Key="CurrColor"></local:myCurrencyColor>
</Window.Resources>

<Grid Name="grid1">        
    <ListBox Margin="28,111,130,24" Name="listBox1" >
        <ListBox.ItemTemplate>
            <DataTemplate>                    
                <Label Content="{Binding Path=FullName}" Background="{Binding Path=Salary, Converter={StaticResource CurrColor}}" ></Label>                                            
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
     // dt.Columns.Add(new DataColumn("FullName", typeof(String)));
     // dt.Columns.Add(new DataColumn("Salary", typeof(Decimal)));

      DataTable dt = GetEmployees();  // it has Salary and Fullname Columns

      grid1.DataContext = dt;
}

.

  [ValueConversion(typeof(decimal), typeof(Brush))]
  public class myCurrencyColor : IValueConverter
  {
       public object Convert(object value, Type tp, object obj, System.Globalization.CultureInfo cul)
       {
            decimal dml = (decimal)value;
            if(dml < 5)
                return new SolidColorBrush(Colors.Aqua);
            if (dml < 10)
                return new SolidColorBrush(Colors.Azure);
            if (dml < 15)
                return new SolidColorBrush(Colors.BurlyWood);
            if (dml < 20)
                return new SolidColorBrush(Colors.Goldenrod);
            return new SolidColorBrush(Colors.HotPink);
        }
        public object ConvertBack(object value, Type tp, object obj, System.Globalization.CultureInfo cul)
        {
            throw new NotImplementedException();
        }
   }
+1  A: 

You are not binding the items to the list.

Try adding the ItemsSource:

<ListBox ItemsSource="{Binding}" 
         Margin="28,111,130,24" 
         Name="listBox1" >
      .....
</ListBox>
Wonko the Sane
A: 

just try with

grid1.ItemsSource=dt.DefaultView;
Kishore Kumar