WPF将ListBox绑定到DataSet

时间:2013-03-31 20:47:56

标签: c# wpf listbox dataset

我正在尝试将数据集显示在ListBox中。这是我到目前为止所得到的:

XAML

<Window x:Class="FilterDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FilterDemo" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1" >
<Window.Resources>
    <local:FilterDemoBackEndDataSet x:Key="FilterDemoBackEndDataSet"/>
    <CollectionViewSource x:Key="cLIENTSViewSource" Source="{Binding CLIENTS, Source={StaticResource FilterDemoBackEndDataSet}}"/>
</Window.Resources>

<Grid>
    <StackPanel Grid.Column="1" Background="#c4d9f9" >
        <DockPanel HorizontalAlignment="Left" Height="20" Width="220"  Margin="10,5,5,5">
            <TextBox Name="txtSearch" Height="20" Width="160" DockPanel.Dock="Left"  />
            <Button Name="cmdFilter" Height="20" Width="20"  Background="Black" DockPanel.Dock="Right"/>
        </DockPanel>

        <ListBox Name="lstAccountNo"  Height="550"  Width="200" HorizontalAlignment="Left" DisplayMemberPath="AccountNo" ItemsSource="{Binding}"
                             Margin="10,5,5,5" SelectedItem="{Binding AccountNo}" IsSynchronizedWithCurrentItem="True" />
    </StackPanel>
</Grid>

Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FilterDemo
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
      }

      private void Window_Loaded_1(object sender, RoutedEventArgs e)
      {
          FilterDemo.FilterDemoBackEndDataSet FilterDemoBackEndDataSet = ((FilterDemo.FilterDemoBackEndDataSet)(this.FindResource("FilterDemoBackEndDataSet")));
          // Load data into the table CLIENTS. You can modify this code as needed.
          FilterDemo.FilterDemoBackEndDataSetTableAdapters.CLIENTSTableAdapter FilterDemoBackEndDataSetCLIENTSTableAdapter = new FilterDemo.FilterDemoBackEndDataSetTableAdapters.CLIENTSTableAdapter();
          FilterDemoBackEndDataSetCLIENTSTableAdapter.Fill(FilterDemoBackEndDataSet.CLIENTS);
          System.Windows.Data.CollectionViewSource cLIENTSViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("cLIENTSViewSource")));
          cLIENTSViewSource.View.MoveCurrentToFirst();
      }
  }
}

我有一个名为FilterDemoBackEnd的数据集,一个名为Clients的表和一个名为AccountNo的字段。当我运行上面的代码时,窗口打开但没有显示任何项目。

请不要回复一个线程的链接,因为我已经搜索了互联网,试图解决这个问题。

XAML很难。

此致

1 个答案:

答案 0 :(得分:0)

通过将数据网格拖动到主窗口然后将其删除来对其进行排序。我不确定这有什么帮助,但它确实有效。