MultiSelect for WPF
データソースへのMultiSelectの連結
データ連結 > データソースへのMultiSelectの連結

To bind MultiSelect for WPF control to a data source, follow these steps:

  1. Create a connection string and fetch data from a database to a data set.
    Private Shared Function GetConnectionString() As String
        Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Common\C1NWind.mdb;"
        Return String.Format(conn)
    End Function
    
    Private Function GetDataSource(ByVal connectionString As String) As DataTable
        '接続文字列を設定します
        Dim conn As String = GetConnectionString()
    
        'SQL ステートメントを設定します
        Dim rs As String = connectionString
    
        'データを DataSet に取得します
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(rs, conn)
        Dim ds As DataSet = New DataSet()
        da.Fill(ds)
    
        'データテーブルを返します
        Return ds.Tables(0)
    
    End Function
    
    static string GetConnectionString()
    {
          string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Common\C1NWind.mdb;";
          return string.Format(conn);
    }
    DataTable GetDataSource(string connectionString)
    {
           // 接続文字列を設定します
           string conn = GetConnectionString();
    
          // SQL ステートメントを設定します
          string rs = connectionString;
    
          // データを DataSet に取得します
          OleDbDataAdapter da = new OleDbDataAdapter(rs, conn);
          DataSet ds = new DataSet();
          da.Fill(ds);
    
          // データテーブルを返します
          return ds.Tables[0];
    }
    
  2. Set the ItemsSource and DisplayMemberPath properties for the MultiSelect control.
    Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        mselect.ItemsSource = GetDataSource("Select * from Employees ").AsDataView()
        mselect.BindingInfo.DisplayMemberPath = "FirstName"
    End Sub
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        mselect.ItemsSource = GetDataSource("Select * from Employees ").AsDataView();
        mselect.BindingInfo.DisplayMemberPath = "FirstName";
    }