neki
neki

Reputation: 23

Wpf Combobox DataBinding

hello everyone it is me again! i've got few problems too. i am deveoping an training software that's why i am asking lots of questions.i hope you help me. thanks in advance. my problems are as follows:

First of all: i have a register window that has a combobox. i have binded it an access datasource. the problem is when i select an item, it doesnt select. it writes System.data.Datarow.(i want it list names like mike,susan ect.)

how can i fix it? where is the problem?

public Register()
{                   
    this.InitializeComponent();
    Select();

}

public void Select()
{

    DataView view;
    OleDbConnection con = new OleDbConnection(connectionstring);
    con.Open();
    string sql = "Select * from UserInformation";
    OleDbCommand cmd = new OleDbCommand(sql, con);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "UserInformation");
    view = ds.Tables[0].DefaultView;
    RegCombo.ItemsSource = view;

    con.Close();
}

XAML Code:

<ComboBox IsSynchronizedWithCurrentItem="True" 
    Margin="0,22.447,46.92,0" SelectedItem="{Binding Path=UserName}"
    VerticalAlignment="Top" Height="29" Grid.Column="3" Grid.Row="1" 
    IsEditable="True" IsDropDownOpen="False" MaxDropDownHeight="266.666666666667" 
    FontSize="16" x:Name="RegCombo" FontWeight="Normal"  >

    <ComboBox.ItemTemplate>
        <DataTemplate>

            <TextBlock Text="{Binding Path=UserName}"></TextBlock>

        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Upvotes: 0

Views: 6061

Answers (2)

kiwipom
kiwipom

Reputation: 7709

You'll need to set the DisplayMemberPath on your ComboBox to be the property on the underlying object you want to see in the ItemsControl

If this is not specified, and you have not overridden the ToString() method on that object, you will just see (what you are now seeing) - the qualified name of the object.

Upvotes: 1

Vivek
Vivek

Reputation: 1

Try this Binding="{Binding RelativeSource={RelativeSource Self}, Path=UserName}"

Upvotes: 0

Related Questions