Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Silverlight combobox DisplayMemberPath?
11 replies. Latest Post by owleabf on November 4, 2009.
(0)
owleabf
Member
20 points
35 Posts
11-04-2009 12:08 PM |
Hi, trying something that should be relatively simple, but isn't working for me. Trying to bind a combobox to a list of objects and set a DisplayMemberPath to display the correct element while binding to the whole object. The following code doesn't kick out any errors and appears to bind 4 items to the source, but doesn't actually display the specified property.
[EDIT - for some reason the code window removed my <CCType> specification off the list. Actual list is public List<CCType> _types;]
XAML:
<ComboBox x:Name="comboCardType" Height="16" Margin="1,0,150,0" Style="{StaticResource ComboBoxGreen}" VerticalAlignment="Center" Grid.Column="1" Grid.Row="2" />
CS:
public partial class PaymentProcessing : Page { private decimal _cost; private int _count; private BillTo_Object _bill; private Card_Object _card; public List _types; public struct CCType { public string ID; public string DisplayString; } public PaymentProcessing() { InitializeComponent(); } // Executes when the user navigates to this page. protected override void OnNavigatedTo(NavigationEventArgs e) { _types = new List(); _types.Add(new CCType() { ID = "001", DisplayString = "Visa" }); _types.Add(new CCType() { ID = "002", DisplayString = "MasterCard" }); _types.Add(new CCType() { ID = "003", DisplayString = "American Express" }); _types.Add(new CCType() { ID = "004", DisplayString = "Discover" }); comboCardType.ItemsSource = _types; comboCardType.DisplayMemberPath = "DisplayString";...
Ardman
Contributor
3436 points
947 Posts
11-04-2009 12:11 PM |
Have you tried moving your DisplayMemberPath into your XAML to see if it works?
11-04-2009 12:21 PM |
No dice, behaves in the same fashion.... also tried (with same results):
11-04-2009 12:23 PM |
Have you tried moving the Types initialisation into the Constructor, and then assign the ItemsSource in your OnNavigatedTo method?
11-04-2009 12:27 PM |
Ardman:Have you tried moving the Types initialisation into the Constructor, and then assign the ItemsSource in your OnNavigatedTo method?
Just gave it a shot, same result... ItemSource has correct values in it, but the control isn't rendering the DisplayMemberPath.
Incidentally, targeting Silverlight 3 in case that matters.
11-04-2009 12:29 PM |
Add to your XAML:
SelectedItem="{Binding DisplayString, Mode=TwoWay}"
11-04-2009 12:34 PM |
Ardman:Add to your XAML: SelectedItem="{Binding DisplayString, Mode=TwoWay}"
Again, same result, also tried setting DisplayMemberPath in same fashion. Current code:
<ComboBox x:Name="comboCardType" DisplayMemberPath="{Binding DisplayString, Mode=TwoWay}" Height="16" Margin="1,0,150,0" Style="{StaticResource ComboBoxGreen}" VerticalAlignment="Center" Grid.Column="1" Grid.Row="2" />
public partial class PaymentProcessing : Page { private decimal _cost; private int _count; private BillTo_Object _bill; private Card_Object _card; public List<CCType> _types; public struct CCType { public string ID; public string DisplayString; } public PaymentProcessing() { _types = new List<CCType>(); _types.Add(new CCType() { ID = "001", DisplayString = "Visa" }); _types.Add(new CCType() { ID = "002", DisplayString = "MasterCard" }); _types.Add(new CCType() { ID = "003", DisplayString = "American Express" }); _types.Add(new CCType() { ID = "004", DisplayString = "Discover" }); InitializeComponent(); } // Executes when the user navigates to this page. protected override void OnNavigatedTo(NavigationEventArgs e) { comboCardType.DisplayMemberPath = "DisplayString"; comboCardType.ItemsSource = _types;
11-04-2009 12:35 PM |
You've assigned DisplayMemberPath and not SelectedItem. DisplayMemberPath needs to stay as "DisplayString" whilst you need to add SelectedItem="{Binding DisplayString, Mode=TwoWay}"
11-04-2009 12:38 PM |
Yep, was just trying both... new XAML (w/codebehind assignment of DisplayMemberPath commented out)
[EDIT - and, sad to say, same result with this]
<ComboBox x:Name="comboCardType" DisplayMemberPath="DisplayString" SelectedItem="{Binding DisplayString, Mode=TwoWay}" Height="16" Margin="1,0,150,0" Style="{StaticResource ComboBoxGreen}" VerticalAlignment="Center" Grid.Column="1" Grid.Row="2">
11-04-2009 1:55 PM |
Can you put all of the Type instantiation and the ItemsSource assignment into your constructor to see if that helps?
11-04-2009 2:03 PM |
Ardman: Can you put all of the Type instantiation and the ItemsSource assignment into your constructor to see if that helps?
Gave it a shot, no difference. The key here seems to be around the binding to a complex object/setting the DisplayMemberPath... I'm binding other Comboboxes in the OnNavigatedTo event to lists of simple strings without any problem.
11-04-2009 3:21 PM |
Simplest answer is the easiest... my class CCType had public strings, not public properties.