2010-10-26 52 views
0

我有一个列表框,我试图绑定DataTable。我调试代码并且DataTable中有数据,但它不显示DataList中的数据。带有ItemTemplate问题的WPF列表框

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

回答

2

您没有将项目绑定到列表。

尝试添加的ItemsSource:

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

只是

grid1.ItemsSource=dt.DefaultView; 
尝试