.NET 2.0 binding method no longer works

Hi guys

In .NET 2.0, I used to bind a combobox like this:

Code Snippet

DataSetClass newDataset = new DataSetClass();

cboGroup.DataSource =newDataSet;

cboGroup.ValueMember ="tblGroups.GroupID";

cboGroup.DisplayMember ="tblGroups.GroupName";

But in .NET 3.0, with a WPF Combobox, theDataSource,ValueMemberandDisplayMemberproperties no longer exist. I'm wondering how I can replicate the above behaviour with the new properties (reading of samples has not cleared up how I can do this in code thus far.)

I know you can databind in Blend, but only to adataset class, not adataset instanceof a dataset class where the instance (newDataset) is generated in code.

Thanks for your help,

Ben

[1135 byte] By [-=B3N=-] at [2008-1-4]
# 1

These still exist, with slightly different semantics.

DataSource = ItemsSource

ValueMember = SelectedValuePath

DisplayMember = DisplayMemberPath

DouglasStockwell at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

Thanks Doug. Is there a similar property for the passwordbox or textbox or label control?

Eg:

Code Snippet

passwordbox.? = mydataset.tblusers;

passwordbox.? = "Password";

I tried passwordbox.password but this just set it to "Password" as a string and not as a fieldname.

With textbox I used to use databindings.add

-=B3N=- at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

This is where you probably need to dig into the binding system.

For a TextBox this might look like:

textBox.DataContext = mydataset.tblusers; textBox.SetBinding(TextBox.TextProperty, new Binding("Password")); 

Label is similar, but you can't actually bind the password of a PasswordBox, presumably for security reasons.

DouglasStockwell at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

Hi guys, anybody able to help me out here?

I have two datatables (tblServers and tblLogins) in a dataset. tblServers has a primary key of ServerName, and tblLogins has a shared primary key of ServerName and Email. There is a foreign key, FK_tblServers_tblLogins, linking the ServerName field of the two tables.

I'm trying to bind a combobox, cboEmail, to the Foreign Key ServerName field (the value) and display in the combobox the Email (the displaymember), as follows:

Code Snippet

cboEmail.ItemsSource = ds.tblLogins //Also tried ds.tblServers, to no avail

cboEmail.SelectedValuePath = "FK_tblServers_tblLogins.ServerName";

cboEmail.DisplayMemberPath = "Email";

In .NET 2.0, I used to do it like this:

Code Snippet

cboEmail.DataSource = ds;

cboEmail.ValueMember = "tblServers.FK_tblServers_tblLogins.ServerName";

cboEmail.DisplayMember = "tblServers.FK_tblServers_tblLogins.Email";

But can't figure out how to do it in .NET 3.0.

Thanks in advance for your help. I really appreciate it!

-=B3N=- at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5

Please have a look, it works as following:

Code Snippet

DataTable sp = myDBAccess.getDataFromDB(Properties.Settings.Default.DBV_TEST); // just a DataTable

DataView dv = sp.DefaultView;

this.cboTest.ItemsSource = dv;

this.cboTest.DisplayMemberPath = "BEZ";

this.cboTest.SelectedValuePath = "ID";

Regards

Sven

shaper at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 6

Thanks Sven

I'm not sure as to where my foreign key and ServerName and Email fit into this however. What is DBC_Test and BEZ and ID?

-=B3N=- at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 7

Hello,

Sorry I also don't know anything about foreign keys. I just had one DataTable to bind to the ComboBox. DBV_TEST is just a String variable which holds the name of a Database View. "BEZ" and "ID" are the names of two columns in the DataTable.

Sven

shaper at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 8
Yeah, I can do it without foreign keys coming in to it, but once they do...I don't know what's changed between .NET 2.0 and 3.0 and can't get it to work.
-=B3N=- at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 9

Hi

Anyone aware of a solution?

-=B3N=- at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 10

Ok, here's the solution:

In XAML:

Code Snippet

ItemsSource="{Binding Path='FK_tblServers_tblLogins'}"

in C#

Code Snippet

cboEmail.DataContext = ds.tblServers;

cboEmail.SelectedValuePath = "ServerName";

cboEmail.DisplayMemberPath = "Email";

-=B3N=- at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified