尝试将数据表与列表框绑定...出了问题
请帮助...我在这里做错了什么?尝试将列表框绑定到数据表。调试后,我看到表中的数据行,但有些它没有绑定到列表框。
供参考。 _这是我当前窗口的名称...
<ListBox Grid.Column="1" ItemsSource="{Binding ElementName=_this, Path=MainCategoriesTable}" HorizontalAlignment="Center" BorderBrush="Transparent" Background="Transparent" x:Name="lbMainCategories">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton Grid.Column="0" Content="{Binding Path=main_category_name}" VerticalAlignment="Center" GroupName="grpMainCategory" x:Name="rdbEnableDisable" />
<Label Grid.Column="1" Width="30" Background="Transparent" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
下面是尝试绑定的属性...
public DataTable MainCategoriesTable
{
get { return _dtMainCategory; }
set { _dtMainCategory = value; }
}
Please help...what am I doing wrong here? Trying to bind listbox to datatable. After debugging, i see data rows in the table but some how it is not binding to listbox.
FYI. _this is the name of my current window...
<ListBox Grid.Column="1" ItemsSource="{Binding ElementName=_this, Path=MainCategoriesTable}" HorizontalAlignment="Center" BorderBrush="Transparent" Background="Transparent" x:Name="lbMainCategories">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton Grid.Column="0" Content="{Binding Path=main_category_name}" VerticalAlignment="Center" GroupName="grpMainCategory" x:Name="rdbEnableDisable" />
<Label Grid.Column="1" Width="30" Background="Transparent" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Below is the property trying to bind with...
public DataTable MainCategoriesTable
{
get { return _dtMainCategory; }
set { _dtMainCategory = value; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 XAML 将数据上下文设置为这后面的代码,这对我来说
在代码后面
是有用的,但我使用 _this 就像您已经成功使用它一样。
在所有 XAML 绑定中设置Presentation.Trace = High。这不是确切的语法,但如果您从演示文稿开始,它应该是显而易见的。
为什么标签上没有绑定。
main_category_name 是公共财产吗?我注意到它是小写的。
For XAML to set the data context tocode behind this is what works for me
in code behind
But I used _this like you have used it successfully.
Set Presentation.Trace = High in the all XAML binding. That is not the exact syntax but if you start with Presentation it should be obvious.
Why no binding on label.
main_category_name is a public property? I notice it is in lower case.
DataTable
的工作方式类似于字典,而不是对象。它不会将列公开为属性,但每个DataRow
公开一个 索引器,可用于获取单元格值。因此,您需要使用索引器语法:UPDATE
另一件让我困扰的事情是您的
MainCategoriesTable
属性不会通知更改。如果它在所有Bindings
初始化后发生更改,它将不起作用(而DependencyProperty
会,因为它总是通知更改)。为了使其工作,您的上下文类必须实现 INotifyPropertyChanged 接口,并且您的属性必须如下所示:DataTable
works like a dictionary, not like an object. It doesn't expose your columns as properties, but eachDataRow
exposes an indexer that can be used to get the cell value. Therefore, you need to use indexer syntax:UPDATE
Another thing that bothers me is that your
MainCategoriesTable
property doesn't notify about changes. If it's changed after allBindings
have been initialized, it won't work (whileDependencyProperty
will, because it always notifies about changes). To make it work, your context class must implementINotifyPropertyChanged
interface and your property must look like this: