使用数据库中的复选框填充 StackPanel
我对 WPF 还很陌生。我有一个页面使用 L2S
显示来自 SQL 数据库的数据。 L2S
返回一个 DataTable
,其中包含特定区域可供选择的所有可用选项。它从数据库返回的每一行都需要是一个复选框
,我想将这些复选框放在stackpanel
中。
我正在考虑将数据绑定到 StackPanel 吗?这感觉不对……我猜我需要循环遍历 DataTable 并为每一行创建复选框项,然后在运行时将它们添加到 StackPanel 中。这是正确的吗?返回 DataTable
是我的问题的一部分吗?
我看到 StackPanel
有一个 DataContext
属性,但我不能只设置它,因为它不知道将每个项目设置为 checkbox
,正确的?
I am quite new to WPF. I have a page that displays data from a SQL database using L2S
. The L2S
returns a DataTable
that contains all the available options to choose from for a specific area. Every row it returns from the DataBase needs to be a checkbox
and I want to put those checkboxes in a stackpanel
.
Am I looking at databinding to the StackPanel
? That feels wrong... I was guessing I need to loop through the DataTable
and create checkbox items for each row and then add them at runtime to the StackPanel
. Is this correct? Is returning a DataTable
part of my problem?
I see that StackPanel
has a DataContext
property but I can't just set that cause it wouldn't know to make each item a checkbox
, correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要一个 ItemsControl。这允许您使用指定的 DataTemplate 呈现一系列项目。您可以在 ItemsControl 中内联执行此操作:
或者从资源显式引用数据模板...更像是:
或者您可以定义一个 DataTemplate 来定义绑定类的外观。 (请注意,如果您的 Linq-to-SQL 投影为匿名类型,则这不是一个选项)类似:
WPF 然后将查找与集合中每个项目的数据类型相匹配的 DataTemplate。请注意,这对于绑定需要不同表示的异构集合非常有帮助。
您可以绑定 Stackpanel 的 DataContext,但没有为每个数据元素重复模板的固有逻辑。它只是为子控件和包含的
{Binding ...}
语句提供上下文。所有处理重复数据的控件均源自 ItemsControl,并通过 ItemsSource 属性获取数据。You likely want an ItemsControl. This allows you to present a series of items using a specified DataTemplate. You can either do this inline of the ItemsControl:
or reference the data template explicitly from a resource... something more like:
Or you can define a DataTemplate that defines the look and feel for your bound class. (Note that if your Linq-to-SQL is projecting into an Anonymous Type, this isn't an option) Something like:
WPF will then look for a DataTemplate that matches the DataType of each of the items in your collection. Note that this can be VERY helpful for binding heterogeneous collections which need different presentations.
You CAN bind the DataContext of the Stackpanel, but there is no inherent logic about repeating a template for each element of data. It simply provides a context for child controls and contained
{Binding ...}
statements. All of the controls that handle repeating data descend from ItemsControl and take their data in through the ItemsSource property.除了 Ben Von Handorf 的回答之外,我认为提到您还可以通过更改
ItemsPanel
来更改 ItemsControl 排列其元素的方式会很有用。例如:这使得 ListBox 水平排列项目而不是垂直排列。如果您希望项目像文本一样“流动”(如果您了解 html,则可以使用
span
),您也可以使用WrapPanel
。 WrapPanel 也可以水平或垂直定向。 (但尽量不要将 WrapPanel 用于大型项目集合,因为它不是虚拟化的,并且会导致一些大的延迟。)您甚至可以创建自己的自定义面板并替换它们。我曾经制作了一个随机排列其项目的面板,并将其用于列表框。我绑定的集合中的每个项目都显示在列表框中的随机位置。每次刷新布局时,项目都会获得新的位置。
In addition to Ben Von Handorf's answer, I thought it would be useful to mention that you can also change the way that an ItemsControl arranges its elements by changing the
ItemsPanel
. For example:This makes the ListBox arrange items horizontally instead of vertically. You could also use a
WrapPanel
if you want the items to "flow" like text (orspan
if you know html). WrapPanel can also be oriented horizontally or vertically. (But try not to use WrapPanel for large collections of items because it's not virtualizing, and will cause some big lag.)You can even create your own custom panels and substitute them. I once made a panel that laid out it's items randomly, and used that for a ListBox. Each item in my bound collection was displayed in a random position within the ListBox. Each time the layout refreshed, the items got a new position.