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