Home » WPFRSS

Multi-Column ComboBox that binds to it's Value and Display Members for the drop down

I'm looking for a MultiColumn ComboBox solution as well. I've seen a lot of people talking about using Templates and I have to admit I'm not entirely comfortable with using Templates yet in WPF.

What I'm looking for specifically is a ComboBox that simply shows a two column drop down that displays the values resolved from DisplayMemberPath and SelectedValuePath for each item of the bound ItemsSource of the ComboBox. Once one of the items is selected I'm looking for the SelectedValue only to be displayed in the Text area.


Below is a crude diagram to explain what I'm looking for.

 __________________________
|CODE3___________________|_|
|CODE1 | Display value 1 | |
|CODE2 | Display value 1 | |
|CODE3 | Display value 1 | |
 __________________________




I've fumbled around using RelativeSource and a few other things but nothing works so far. If anybody has a clue about different ways to implement this I would love to hear them. A quick example using templates would be wonderful but also note that I'm planning probably to make this a reusable control in our solution since this is a common paradigm in our UI that I wouldn't want to have to repeat on each Window or UserControl in various projects.
 

5 Answers Found

 

Answer 1

<Page xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
  xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Page.Resources>
<Style TargetType="ComboBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{TemplateBinding Tag}" Width="100"/>
<TextBlock Text="|"/>
<TextBlock Text="{TemplateBinding Content}" Width="100"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid>

<ComboBox x:Name="box" Width="200" VerticalAlignment="Top" HorizontalAlignment="Left">
<ComboBoxItem Tag="Value 1" Content="Something 1"/>
<ComboBoxItem Tag="Value 2" Content="Something 2"/>
<ComboBoxItem Tag="Value 3" Content="Something 3"/>
<ComboBoxItem Tag="Value 4" Content="Something 4"/>
<ComboBoxItem Tag="Value 5" Content="Something 5"/>
</ComboBox>
<TextBlock Text="{Binding ElementName=box, Path=SelectedValue.Tag}"
  HorizontalAlignment="Center"/>
</Grid>
</Page>
Bigsby, Lisboa, Portugal
 

Answer 2

Well, that looks somewhat what I'm looking for except I'm looking to bind to some data to the ComboBox using the ItemSource property and then having my template filled out in a way that somehow uses the resolved values  from DisplayMemberPath and SelectedValuePath so that they can be used to display  in the ComboBox drop  down.

Currently I'm doing my data binding in C# which seems to be working fine after a bit of research and tweaking. Below is an example  of how I'm binding my ComboBox controls.  The XAML is not very interesting since I'm doing the binding here so I've left it out for now.

myWindow.DataContext = myDataSet.Tables["Book"]; 
// Discount binding
cmbDiscCode.DisplayMemberPath = "description"
cmbDiscCode.SelectedValuePath = "bookTypeId"
cmbDiscCode.SetBinding(ComboBox.ItemsSourceProperty, new Binding() { Source = myDataSet.Tables["BookType"] }); 
// Bind the selected value to the inherited context myDataSet.Tables["Book"]
cmbDiscCode.SetBinding(ComboBox.SelectedValueProperty, new Binding("bookTypeId")); 

Then another requirement is that I want to only show the value member (the value resolved  from SelectedValuePath) in the text  portion of the ComboBox. From what I understand I should be able to achieve this by binding directly to the Text property of the ComboBox but the question that I have is what should I bind to this property? I've tried using a relative source binding to bind the SelectedValue property to the Text property but that didn't seem to work and not only that but it raised some concern when I was thinking it through about possible circular traps when doing data binding, so now I have to do some experiments to see what the actual danger is with that :(

If you could elaborate further about how to achieve these specific things  namely Binding template content to the resolved values of DisplayMemberPath and SelectedValuePath or how to bind the selected  value to the Text property I think thats more of what I'm looking for.
 

Answer 3

Hi,

I have been posted an example  at http://yasser-zamani.spaces.live.com/blog/cns!5AAB8D00414B403D!334.entry (Walkthrough: Displaying multi column  ComboBox by using Windows Presentation Foundation (WPF) data templating)

Regards,

Yasser.

 

Answer 4

Hi,

I'm having a problem accessing the value of a multi-column  combo box that's linked to database record.  How do you access the first column  of a combo box that contains a LINQ class called 'user' with fields such as 'userID', 'LastName', 'FirstName', 'Title'.  I display  each field but only want to use the recordID as the foreign key of a new record in another table. I can access the textboxes directly because it contains a string.

Sorry if this question is trivial, but I'm new to C#, LINQ, and pretty much programming in general.

Any help will be appreciated!

privatevoid addNewRequest()

{

 

RequestForm rf = newRequestForm();

rf.Title =

"New Change Request for " + user.FirstName + " " + user.LastName;

 

if (rf.ShowDialog().Value)

{

 

Request newRequest = newRequest();

newRequest.userID = user.userID;

newRequest.systemID =

int.Parse(rf.cmbServer.SetBinding(ComboBox.SelectedValueProperty, "systemID")); //problem

newRequest.statusID =

int.Parse(rf.cmbStatus.SetBinding(ComboBox.SelectedValueProperty, "statusID"));  //problem

newRequest.description = rf.txtDescription.Text;

 

try

{

requestInfo.Add(newRequest);

}

 

catch (ArgumentOutOfRangeException e)

{

}

 

 

this.btnSaveChanges.IsEnabled = true;

}

}

 

  

 

Answer 5

This helps only when we want to view all the columns in the combobox  dropdown. But how to get only the one column  to show as a display  member and other column to be the value member.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter