如何将数据表绑定到wpf中的LISTBOX?

发布于 2024-12-16 11:21:11 字数 100 浏览 1 评论 0原文

如何将数据表绑定到 ListBox 并

选择要在 WPF 的 ListBox 中显示的字段。

你能帮忙讨论一下这个话题吗?

感谢您的关注。

How can i bind datatable to ListBox and

and select a field which I want to display in the ListBox in WPF.

Can you help about this topic?

Thank you for your attention.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

初雪 2024-12-23 11:21:11

你可以试试这个......

<Window x:Class="BindToAdoDataDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
  <DockPanel>
    <Button Width="120" Height="30" Content="Add" Name="btn" DockPanel.Dock="Top"/>
    <ListBox ItemsSource="{Binding}" DisplayMemberPath="ChildItem"/>
  </DockPanel>
</Window>

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        DataTable dataTable = MakeChildTable();
        this.DataContext = dataTable.Rows;
        this.btn.Click += delegate
        {
            DataRow row = dataTable.NewRow();
            row["childID"] = 50;
            row["ChildItem"] = "ChildItem " + 50;
            dataTable.Rows.Add(row);    
        };
    }

    private DataTable MakeChildTable()
    {
        DataTable table = new DataTable("childTable");
        DataColumn column;
        DataRow row;

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "ChildID";
        column.Caption = "ID";

        table.Columns.Add(column);

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "ChildItem";
        column.Caption = "ChildItem";
        table.Columns.Add(column);

        for (int i = 0; i <= 4; i++)
        {
            row = table.NewRow();
            row["childID"] = i;
            row["ChildItem"] = "Item " + i;
            table.Rows.Add(row);
        }

        return table;
    }
}

You can try this ....

<Window x:Class="BindToAdoDataDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
  <DockPanel>
    <Button Width="120" Height="30" Content="Add" Name="btn" DockPanel.Dock="Top"/>
    <ListBox ItemsSource="{Binding}" DisplayMemberPath="ChildItem"/>
  </DockPanel>
</Window>

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        DataTable dataTable = MakeChildTable();
        this.DataContext = dataTable.Rows;
        this.btn.Click += delegate
        {
            DataRow row = dataTable.NewRow();
            row["childID"] = 50;
            row["ChildItem"] = "ChildItem " + 50;
            dataTable.Rows.Add(row);    
        };
    }

    private DataTable MakeChildTable()
    {
        DataTable table = new DataTable("childTable");
        DataColumn column;
        DataRow row;

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "ChildID";
        column.Caption = "ID";

        table.Columns.Add(column);

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "ChildItem";
        column.Caption = "ChildItem";
        table.Columns.Add(column);

        for (int i = 0; i <= 4; i++)
        {
            row = table.NewRow();
            row["childID"] = i;
            row["ChildItem"] = "Item " + i;
            table.Rows.Add(row);
        }

        return table;
    }
}
给我一枪 2024-12-23 11:21:11

这是一个纯 XAML 解决方案:

<DockPanel DataContext="{StaticResource myDataViewSource}">

    <ComboBox DockPanel.Dock="Bottom" x:Name="FieldSelector" SelectedIndex="0">
           <ComboBoxItem Content="Field1" Tag="Field1"/>
           <ComboBoxItem Content="Field2" Tag="Field2"/>
           <ComboBoxItem Content="Field3" Tag="Field3"/>
           <ComboBoxItem Content="Field4" Tag="Field4"/>
    </ComboBox>

    <ListBox ItemsSource="{Binding}"
             DisplayMemberPath="{Binding ElementName=FieldSelector,Path=SelectedItem.Tag}" />

</DockPanel>

Here is a pure XAML solution :

<DockPanel DataContext="{StaticResource myDataViewSource}">

    <ComboBox DockPanel.Dock="Bottom" x:Name="FieldSelector" SelectedIndex="0">
           <ComboBoxItem Content="Field1" Tag="Field1"/>
           <ComboBoxItem Content="Field2" Tag="Field2"/>
           <ComboBoxItem Content="Field3" Tag="Field3"/>
           <ComboBoxItem Content="Field4" Tag="Field4"/>
    </ComboBox>

    <ListBox ItemsSource="{Binding}"
             DisplayMemberPath="{Binding ElementName=FieldSelector,Path=SelectedItem.Tag}" />

</DockPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文