使用C#winforms创建和填充自定义列表外观
考虑自定义列表元素的用例,它有多行和其他数据元素,如图片。所以,如果我有一个数据实例列表,我如何使用数据源来填充自定义列表。我还想将列表绑定到display元素 样本类 个人努力 目前,我正在使用datagridview使用C#winforms创建和填充自定义列表外观,c#,winforms,c#-4.0,custom-controls,C#,Winforms,C# 4.0,Custom Controls,考虑自定义列表元素的用例,它有多行和其他数据元素,如图片。所以,如果我有一个数据实例列表,我如何使用数据源来填充自定义列表。我还想将列表绑定到display元素 样本类 个人努力 目前,我正在使用datagridview private BindingSource bs = new BindingSource(); // ... bs.DataSource = typeof(BeerData); foreach (BeerData entry in new BeerData[]{ new Be
private BindingSource bs = new BindingSource();
// ...
bs.DataSource = typeof(BeerData);
foreach (BeerData entry in new BeerData[]{ new BeerData()
{
reiting = 4.2,
name = "Bell's Hopslam Ale",
brewery = "Bell's Brewery, Inc",
beer = "Imperiial IPA",
image = "http://upload.wikimedia.org/wikipedia/" +
"commons/6/60/Kriek_Beer_1.jpg"
}/* , ... */})
bs.Add(entry);
grid.DataSource = bs;
grid.AutoGenerateColumns = true;
lName.DataBindings.Add(new Binding("Text", bs, "name"));
lImage.DataBindings.Add(new Binding("ImageLocation", bs, "image"));
// ...
个人努力2
公共静态类BeerLoader
{
公共静态列表结果=新列表();
静态装载机(){
add1BogusDataInstance();
}
公共静态void add1BogusDataInstance()
{
foreach(新BeerData[]{new BeerData()中的BeerData条目)
{
reiting=4.2,
name=“Bell's Hopslam Ale”,
brewery=“贝尔啤酒厂有限公司”,
啤酒=“帝国IPA”,
imageUrl=”http://upload.wikimedia.org/wikipedia/" +
“commons/6/60/Kriek_Beer_1.jpg”
}})
结果。添加(条目);
}
公共静态列表LoadData()
{
返回结果;
}
}
期望结果的样本
替换datagridview的列表部分示例
private BindingSource bs = new BindingSource();
// ...
bs.DataSource = typeof(BeerData);
foreach (BeerData entry in new BeerData[]{ new BeerData()
{
reiting = 4.2,
name = "Bell's Hopslam Ale",
brewery = "Bell's Brewery, Inc",
beer = "Imperiial IPA",
image = "http://upload.wikimedia.org/wikipedia/" +
"commons/6/60/Kriek_Beer_1.jpg"
}/* , ... */})
bs.Add(entry);
grid.DataSource = bs;
grid.AutoGenerateColumns = true;
lName.DataBindings.Add(new Binding("Text", bs, "name"));
lImage.DataBindings.Add(new Binding("ImageLocation", bs, "image"));
// ...
形象
问题:
我假设,我需要创建一个
用户控件
,然后创建该用户控件的列表或类似的东西。无法使用google找到winform示例,因此我想知道这是如何实现的。您显示“列表”的方式使我认为您可能会尝试使用列表框,其中DrawMode=OwnerDrawFixed和ItemHeight=64(或其他)。然后在ListBox的DrawItem事件中,可以根据需要绘制输出
如果您需要使用DataGridView,那么我将查看CellPaint事件并执行类似的操作。如果我使用wpf DataTemplate of:
<Border HorizontalAlignment="Left" BorderThickness="2" Margin="3"
Padding="2" CornerRadius="2" BorderBrush="Black" >
<StackPanel Orientation="Horizontal" >
<Border HorizontalAlignment="Left" BorderThickness="1" Margin="3"
CornerRadius="1" BorderBrush="Gray" VerticalAlignment="Top">
<Image Width="128"
Source="{Binding Path=imageUrl}" ></Image>
</Border>
<StackPanel Width="120">
<Label Content="{Binding Path=name}"/>
<Label Content="{Binding Path=brewery}"/>
<Label Content="{Binding Path=beer}"/>
<StackPanel Width="120" Orientation="Horizontal" >
<Label Content="AVG Rating: "/>
<Label Content="{Binding Path=reiting}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
使用静态数据提供程序和gui:
<Window.DataContext>
<ObjectDataProvider
ObjectType="{x:Type local:BeerLoader}" MethodName="LoadData" />
</Window.DataContext>
<StackPanel>
<Button Content="Add" Click="Button_Click" />
<ListBox Name="data" ItemsSource="{Binding}" />
</StackPanel>
我可以得到:
什么是更好更清晰的
然而,数据不是静态的,我还没有找到解决方法
在C#代码中,我只需要将集合从
List
更改为observedcollection
您所要做的就是…嗯,啤酒…问题是什么?它需要是winforms吗?使用WPF会容易得多。
<Window.DataContext>
<ObjectDataProvider
ObjectType="{x:Type local:BeerLoader}" MethodName="LoadData" />
</Window.DataContext>
<StackPanel>
<Button Content="Add" Click="Button_Click" />
<ListBox Name="data" ItemsSource="{Binding}" />
</StackPanel>