免责声明:是的,我知道 OP 的图片显示 WinForms,但在另一个问题中,OP 表示他们正在关注YouTube
. 我想我会提供另一种方式,他可以使用 WPF 表示相同的数据。
您可以使用 aModel
来显示您的每一个DataTypes
,而不是处理Raw Data
; 看看Object-Relational Mapping,比如 Entity-Framework、NHibernate 等。
创建一个Product
模型来保存Product
表中的数据。
public class Product {
public int ProductID {get;set;}
public int ProductType { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public byte[] Image { get; set; }
}
创建一个Product Type
模型来保存Product Type
表中的数据。
public class ProductType {
public int ProductTypeID { get; set; }
public string Description { get; set; }
}
创建一个Window
/UserControl
来保存你的TabControl
.
<Window.Resources>
<local:ProductConverter x:Key="ProductConverter" />
<local:ProductTypeConverter x:Key="ProductTypeConverter" />
</Window.Resources>
<TabControl ItemsSource="{Binding MyProducts,
Converter={StaticResource ProductConverter}}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource ProductTypeConverter}">
<Binding Path="ProductType"/>
<Binding Path="DataContext.MyProductTypes"
RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Width="150" Content="{Binding Description}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
使用ItemTemplate
andContentTemplate
将确保您的所有Product
对象都具有相同的格式和样式。
这是根据值将您的整个列表转换Products
为组的转换器。Products
Product Type
public class ProductConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if(value as List<Product> != null) {
return (value as List<Product>).GroupBy(a => new { a.ProductType });
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return null;
}
}
这是转换器,而不是将Product.ProductType
值转换为ProductType.Description
public class ProductTypeConverter : IMultiValueConverter {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if(values[0] != null && values[0] != DependencyProperty.UnsetValue &&
values[1] != null && values[1] != DependencyProperty.UnsetValue) {
string f= (values[1] as List<ProductType>)
.Where(a => a.ProductTypeID.Equals(values[0]))
.First().Description;
return f;
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
return null;
}
}
创建用于显示TabControl.Items
.
public List<Product> MyProducts { get; set; }
public List<ProductType> MyProductTypes { get; set; }
之后,您所要做的就是Raw Data
以模型格式表示您的。(我的 SQL 有点不确定)
SqlCommand sqlCmdProducts = new SqlCommand("SELECT * FROM TblProduct", myConnection);
SqlDataReader productReader = sqlCmdProducts.ExecuteReader();
while (productReader.Read()) {
MyProductTypes.Add(new ProductType() {
ProductTypeID = Int32.Parse(productReader["ProductType"].ToString()),
Description = Int32.Parse(productReader["Description"].ToString()),
};
}
SqlCommand sqlCmdProductType = new SqlCommand("SELECT * FROM TblProductType", myConnection);
SqlDataReader productTypeReader = sqlCmdProductType.ExecuteReader();
while (productTypeReader.Read()) {
MyProducts.Add(new Product() {
ProductID = Int32.Parse(productTypeReader["ProductID"].ToString()),
ProductType = Int32.Parse(productTypeReader["ProductType"].ToString()),
Description = productTypeReader["Description"].ToString()),
Price = double.Parse(productTypeReader["Price"].ToString()),
};
}