Skip to main content

Microsoft Silverlight

Answered Question Silverlight combobox DisplayMemberPath?RSS Feed

(0)

owleabf
owleabf

Member

Member

20 points

35 Posts

Silverlight combobox DisplayMemberPath?

 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
Ardman

Contributor

Contributor

3436 points

947 Posts

Re: Silverlight combobox DisplayMemberPath?

Have you tried moving your DisplayMemberPath into your XAML to see if it works?

owleabf
owleabf

Member

Member

20 points

35 Posts

Re: Silverlight combobox DisplayMemberPath?

 No dice, behaves in the same fashion.... also tried (with same results):

  •   moving DisplayMemberPath assignment in front of itemsource assignment
  •  changing from itemssource to datacontext
  • setting a datatemplate in the combobox with a textblock bound to DisplayString (w/o DisplayMemberPath set, with it set causes crash)

Ardman
Ardman

Contributor

Contributor

3436 points

947 Posts

Re: Re: Silverlight combobox DisplayMemberPath?

Have you tried moving the Types initialisation into the Constructor, and then assign the ItemsSource in your OnNavigatedTo method?

owleabf
owleabf

Member

Member

20 points

35 Posts

Re: Re: Silverlight combobox DisplayMemberPath?

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.

Ardman
Ardman

Contributor

Contributor

3436 points

947 Posts

Re: Re: Re: Silverlight combobox DisplayMemberPath?

Add to your XAML:

SelectedItem="{Binding DisplayString, Mode=TwoWay}"

owleabf
owleabf

Member

Member

20 points

35 Posts

Re: Re: Re: Silverlight combobox DisplayMemberPath?

 

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;

Ardman
Ardman

Contributor

Contributor

3436 points

947 Posts

Re: Re: Re: Re: Silverlight combobox DisplayMemberPath?

You've assigned DisplayMemberPath and not SelectedItem.  DisplayMemberPath needs to stay as "DisplayString" whilst you need to add SelectedItem="{Binding DisplayString, Mode=TwoWay}"

owleabf
owleabf

Member

Member

20 points

35 Posts

Re: Re: Re: Silverlight combobox DisplayMemberPath?

 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">
 

Ardman
Ardman

Contributor

Contributor

3436 points

947 Posts

Re: Re: Re: Re: Silverlight combobox DisplayMemberPath?

 Can you put all of the Type instantiation and the ItemsSource assignment into your constructor to see if that helps?

owleabf
owleabf

Member

Member

20 points

35 Posts

Re: Re: Re: Re: Silverlight combobox DisplayMemberPath?

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.

owleabf
owleabf

Member

Member

20 points

35 Posts

Answered Question

Re: Silverlight combobox DisplayMemberPath?

 Simplest answer is the easiest... my class CCType had public strings, not public properties.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities