views:

246

answers:

2

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>
A: 

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

Vivek
That will look for a UserName property on the TextBlock itself. RelativeSource Self refers to the FrameworkElement on whose property the Binding is being set.
itowlson
+1  A: 

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.

IanR
Suggest clarifying that if he sets DMP he does not need the ItemTemplate any more (in fact I think ItemTemplate overrides DMP and would therefore have to be removed for DMP to work).
itowlson
thanks LanR, i did what you said. i solved the problems. thank you very much
neki