2010-10-26 5 views
0

J'ai une liste et j'essaye de lier le DataTable avec lui. Je débogue le code et il y a des données dans le DataTable mais il n'affiche pas les données dans le DataList.WPF ListBox avec le problème ItemTemplate

<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(); 
     } 
    } 

Répondre

2

Vous ne liez pas les éléments à la liste.

Essayez d'ajouter ItemsSource:

<ListBox ItemsSource="{Binding}" 
     Margin="28,111,130,24" 
     Name="listBox1" > 
     ..... 
</ListBox> 
0

juste essayer avec

grid1.ItemsSource=dt.DefaultView; 
Questions connexes