开发者

Binding Combobox SelectedValue to Page data value

开发者 https://www.devze.com 2023-03-17 15:00 出处:网络
BASE = C#, .Net, MVC, Silverlight 4 views I\'ve looked at responses to similar problems and I still can\'t get it to work. I guess it\'s not as simple a problem as I first thought.

BASE = C#, .Net, MVC, Silverlight 4 views

I've looked at responses to similar problems and I still can't get it to work. I guess it's not as simple a problem as I first thought.

I have a class AgreementInfo that contains, among other things, a ProjectProponent property

. . . 
public Responsible_Person ProjectProponent { get; set; }
. . . 

which is of type Responsible_Person

   . . .
   public string LASTNAME { get { return m_last; } set { m_last = value; } }
   public string USERNAME { get { return m_uname; } set { m_uname = value; } }
   public string FIRSTNAME { get { return m_first; } set { m_first = value; } }
   public string FullName { get { return m_first + " " + m_last; } }
   . . .

When the SL control loads it fetches, via json, an AgreementInfo object from the controller. This object is set as the DataContext for the LayoutRoot.

Then, I have a combobox whose ItemSource is set to a List. Currently, it is dummy data hard-wired in like

   . . .
   List<Responsible_Person> items = new List<Responsible_Person>();

   Responsible_Person newguy = new Responsible_Person();
   newguy.FIRSTNAME = "Jane"; newguy.LASTNAME = "Dough"; newguy.USERNAME = "jdough"; newguy.RP_ROLES = dummyroles;
   items.Add(newguy);
   . . . 

   cboProjectProponent.ItemsSource = items;
   cboProjectProponent.DisplayMemberPath = "FullName";

Eventually the list will be obtained from a service.

PROBLEM: In the XAML I have

. . .
<TextBlock x:Name="tbktest" Text="{Binding ProjectProponent.FullName}" />
<ComboBox x:Name="cboProjectProponent" SelectedValue="{Binding ProjectProponent.FullName, Mode=TwoWay}" />
. . . 

I am guessing that I see the correct name from AgreementInfo in the text block but the same name, which does appear in the drop-down list, is not selected beca开发者_JAVA百科use I am binding to the wrong thing. What I want is the combobox loaded with values from the List and the ProjectProponent from the AgreementInfo to be the selected item.


Is your ItemSource set before the binding? If the binding occurs before the ItemSource is set then the SelectedValue binding will not work. I have seen these types of timing issues before. Can you not set the ItemSource via XAML binding rather than in code?

I notice you are also not setting the SelectedValuePath property of the combo. Without this you are attempting to assign a Responsible_Person to ProjectProponent.FullName (the SelectedValue). Check this article out for a good explanation of all the ComboBox control properties: http://johnpapa.net/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath

0

精彩评论

暂无评论...
验证码 换一张
取 消