尝试将数据表与列表框绑定...出了问题

发布于 2024-12-23 06:22:12 字数 1119 浏览 2 评论 0原文

请帮助...我在这里做错了什么?尝试将列表框绑定到数据表。调试后,我看到表中的数据行,但有些它没有绑定到列表框。

供参考。 _这是我当前窗口的名称...

            <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-12-30 06:22:12

对于 XAML 将数据上下文设置为这后面的代码,这对我来说

  DataContext="{Binding RelativeSource={RelativeSource Self}}"

在代码后面

  this.DataContext = this;

是有用的,但我使用 _this 就像您已经成功使用它一样。

在所有 XAML 绑定中设置Presentation.Trace = High。这不是确切的语法,但如果您从演示文稿开始,它应该是显而易见的。

为什么标签上没有绑定。

main_category_name 是公共财产吗?我注意到它是小写的。

For XAML to set the data context tocode behind this is what works for me

  DataContext="{Binding RelativeSource={RelativeSource Self}}"

in code behind

  this.DataContext = this;

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.

雨后彩虹 2024-12-30 06:22:12

DataTable 的工作方式类似于字典,而不是对象。它不会将列公开为属性,但每个 DataRow 公开一个 索引器,可用于获取单元格值。因此,您需要使用索引器语法:

<RadioButton Grid.Column="0" Content="{Binding Path=[main_category_name]}" VerticalAlignment="Center" GroupName="grpMainCategory" x:Name="rdbEnableDisable" />

UPDATE

另一件让我困扰的事情是您的 MainCategoriesTable 属性不会通知更改。如果它在所有 Bindings 初始化后发生更改,它将不起作用(而 DependencyProperty 会,因为它总是通知更改)。为了使其工作,您的上下文类必须实现 INotifyPropertyChanged 接口,并且您的属性必须如下所示:

public DataTable MainCategoriesTable
{
    get { return _dtMainCategory; }
    set
    {
      if(value == _dtMainCategory)
      {
        return;
      }

      _dtMainCategory = value;
      var h = this.PropertyChanged;
      if(h != null)
      {
        h(this, new PropertyChangedEventArgs("MainCategoriesTable"));
      }
    }
}

DataTable works like a dictionary, not like an object. It doesn't expose your columns as properties, but each DataRow exposes an indexer that can be used to get the cell value. Therefore, you need to use indexer syntax:

<RadioButton Grid.Column="0" Content="{Binding Path=[main_category_name]}" VerticalAlignment="Center" GroupName="grpMainCategory" x:Name="rdbEnableDisable" />

UPDATE

Another thing that bothers me is that your MainCategoriesTable property doesn't notify about changes. If it's changed after all Bindings have been initialized, it won't work (while DependencyProperty will, because it always notifies about changes). To make it work, your context class must implement INotifyPropertyChanged interface and your property must look like this:

public DataTable MainCategoriesTable
{
    get { return _dtMainCategory; }
    set
    {
      if(value == _dtMainCategory)
      {
        return;
      }

      _dtMainCategory = value;
      var h = this.PropertyChanged;
      if(h != null)
      {
        h(this, new PropertyChangedEventArgs("MainCategoriesTable"));
      }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文