0

i want to add combobox items and use any other controls in module but when i try to use my combobox there, is not recognized. In Window Form Application there was no problem, but in a WPF application I do not know now how to do it?

In WinForm App i do in Module.vb something like that

 Sub FillComboBox()

        Dim SQLStr As String = "use testowa Select COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'Import')"
        Dim Reader As SqlDataReader
        Dim cmd As New SqlCommand(SQLStr, myConnection)

        Form2.ComboBox7.Items.Add("None")
        Form2.ComboBox3.Items.Add("None")


        Reader = cmd.ExecuteReader()

        While Reader.Read()
            Form2.ComboBox1.Items.Add(Reader.Item("COLUMN_NAME"))
            Form2.ComboBox2.Items.Add(Reader.Item("COLUMN_NAME"))
            Form2.ComboBox4.Items.Add(Reader.Item("COLUMN_NAME"))
            Form2.ComboBox5.Items.Add(Reader.Item("COLUMN_NAME"))
            Form2.ComboBox6.Items.Add(Reader.Item("COLUMN_NAME"))
            Form2.ComboBox7.Items.Add(Reader.Item("COLUMN_NAME"))
            Form2.ComboBox3.Items.Add(Reader.Item("COLUMN_NAME"))
        End While

        Reader.Close()


    End Sub

Now i need to do the same thing in WPF app.

Please help, these are my first steps in WPF :)

2 Answers 2

1
  • fill a collection (eg. List of string) from your SQL
  • set this collection as the ItemsSource for your combobox

thats all

ps: pls read something about binding in wpf. dont try to code your winform style with wpf.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's a simple example to show you the pattern:

XAML:

<Window x:Class="EmptyWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" MinWidth="200">
    <Grid>
        <ComboBox ItemsSource="{Binding myList}" />  
    </Grid>
</Window>

MainWindow.cs:

public partial class MainWindow : Window
{
    private List<string> _mylist;
    public List<string> myList
    {
        get
        {
            return _mylist;
        }
        set
        {
            _mylist = value;
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        _mylist = new List<string>() { "Item1", "Item2", "Item3" };
        DataContext = this;
    }
}

If you need to change the collection while the application is running and you would like the combobox to react to the changes, use an ObservableCollection instead of a List.

A good starting point if you never touched WPF is www.wpftutorial.net

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.