Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

DataFormComboBoxField RSS

12 replies

Last post Jun 11, 2009 05:28 PM by RogerGu

(0)
  • jpy

    jpy

    Member

    6 Points

    8 Posts

    DataFormComboBoxField

    May 05, 2009 01:34 AM | LINK

    Does anyone have some (or links to) sample code or examples of how to use the DataFormComboBoxField control within the dataform? I haven't had much luck with binding, etc.

  • bbakermai

    bbakermai

    Member

    230 Points

    151 Posts

    Re: DataFormComboBoxField

    May 06, 2009 10:27 PM | LINK

    I blogged about it in March here. I've since converted the multiple IValueConverter's I mentioned there into a single IValueDictionary approach and just finished that post here.

    Rocky Lhotka also blogged about a general extension to the basic Silverlight ComboBox here that I hope the team reads and incorporates into the RTW. Silverlight has been missing a DisplayMemberPath for too long.

    Silverlight 3 RIA Services DataBinding

    If this has answered your question, please hit the Mark as Answered link. Thanks!

    Bob Baker
    MicroApplications, Inc.
    Orlando .Net Users' Group
  • jpy

    jpy

    Member

    6 Points

    8 Posts

    Re: DataFormComboBoxField

    May 11, 2009 12:58 PM | LINK

    Appreciate it, will review and see what I find. Combo box in 2.0 has been a true pain point, really hoping they iron out the kinks in 3.0.

  • RogerGu

    RogerGu

    Participant

    1124 Points

    420 Posts

    Re: DataFormComboBoxField

    Jun 02, 2009 09:42 PM | LINK

    Bob, Great stuff. I downloaded the sample, and it runs well. I am trying to apply the same pattern to a project I am working on, but I am stuck on the part where you set the itemsource of the DataFormComboBoxField. You are using the Loaded event of the context. Obvoiusly that works. My issue is that I actually have my context fully loaded before I get to the page where I have a DataForm. I have tried setting it in code behind, but the DataFormComboBoxField itself is always null. I have tried attaching to the Loaded events of both my page, and the DataForm, I even tried the ContentLoaded event for the DataForm, but the DataFormComboBoxField is null. Ideas?

    Is there a way to declaratively set the ItemSource of the DataFormComboBoxField? I have not had any luck with StaticResources.

    Thanks,

    DataFormComboBoxField ItemSource

  • bbakermai

    bbakermai

    Member

    230 Points

    151 Posts

    Re: DataFormComboBoxField

    Jun 03, 2009 03:10 PM | LINK

    In the Page Loaded event, you should be able to check the count of the object in the context you need for the DataFormComboBoxField. If the required context object has (loaded) members, you should then be able to 1) set up the dictionary for the binding, and 2) call the find control code in the sample, and, 3) set the itemssource and converter parameters to the found DataFormComboBoxField. Maybe I'm not understanding what you are trying to do differently from the sample.

    If this has answered your question, please hit the Mark as Answered link. Thanks!

    Bob Baker
    MicroApplications, Inc.
    Orlando .Net Users' Group
  • RogerGu

    RogerGu

    Participant

    1124 Points

    420 Posts

    Re: DataFormComboBoxField

    Jun 03, 2009 03:49 PM | LINK

    Here is an example. I created this to reproduce the issue. In this case, it is as if we don't have to wait for our context to load the objects. They are immediately available. In your sample, you took advantage of the loaded even on the context to set teh DataFormComboBoxField item source. In this case, we already have what we need to populate the DataFormComboBoxField, but it is null at any point I check it. There is something simple I am missign. In my actual applicaiton, the context is completely loaded before my page is loaded, and I just need to set the ItemsSource of a DataFormComboBoxField with what is already in the context:

     

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    
    namespace BusinessApplicationTest
    {
        public partial class HomePage : Page
        {
            public User thisUser { get; set; }
            public List teams { get; set; }
    
            public HomePage()
            {
                InitializeComponent();
    
                teams = new List();
                for (int i = 1; i < 6; i++)
                {
                    teams.Add(new Team(i, string.Format("Team{0}", i)));
                }
    
                thisUser = new User("Me", teams[2], DateTime.Now);
    
                formDetail.CurrentItem = thisUser;
                //cboStatus.ItemsSource = teams;
    
                this.Loaded += new System.Windows.RoutedEventHandler(HomePage_Loaded);
                formDetail.Loaded += new System.Windows.RoutedEventHandler(formDetail_Loaded);
            }
    
            void formDetail_Loaded(object sender, System.Windows.RoutedEventArgs e)
            {
                Debug.WriteLine(cboStatus); //null
                //cboStatus.ItemsSource = teams;
            }
    
            void HomePage_Loaded(object sender, System.Windows.RoutedEventArgs e)
            {
                Debug.WriteLine(cboStatus); //null
                //cboStatus.ItemsSource = teams;
            }
    
            // Executes when the user navigates to this page.
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
        }
    
        public class User
        {
            public string Name { get; set; }
            public Team AssociatedTeam { get; set; }
            public DateTime DateJoined { get; set; }
    
            public User(string name, Team team, DateTime dateJoined)
            {
                Name = name;
                AssociatedTeam = team;
            }
        }
    
        public class Team
        {
            public int TeamId { get; set; }
            public string Name { get; set; }
    
            public Team(int id, string name)
            {
                TeamId = id;
                Name = name;
            }
        }
    }
    
     

  • davidezordan

    davidezordan

    Contributor

    6294 Points

    957 Posts

    Re: Re: DataFormComboBoxField

    Jun 03, 2009 08:50 PM | LINK

    @ Roger,

    you can also use ObservableCollection<> to make it works:

    public class Team

    {

    public int TeamID { get; set; }

    public string Value { get; set; }

    }

    public class ObservableTeams : ObservableCollection<Team>

    {

    public ObservableTeams()

    {

    this.Add(new Team { TeamID = 1, Value = "Team 1" });

    this.Add(new Team { TeamID = 2, Value = "Team 2" });

    }

    }

     

    and your xaml:

    <Grid.Resources>

    <local:ObservableTeams x:Key="ObservableTeams"/>

    </Grid.Resources>

    .....

    <dataControls:DataFormComboBoxField x:Name="cboTeamsTest" ItemsSource="{StaticResource ObservableTeams}" DisplayMemberPath="Value" FieldLabelContent="TeamTest:"/>

    Hope this helps.

    Thanks, Davide

    Silverlight MVP

    Blog Twitter Silverlight Experts
  • RogerGu

    RogerGu

    Participant

    1124 Points

    420 Posts

    Re: Re: DataFormComboBoxField

    Jun 03, 2009 10:52 PM | LINK

    Ok, thanks. what I did, to have this available globally is create this:

        public class ObservableUserStatus : ObservableCollection
        {
            public ObservableUserStatus()
            {
                context.Loaded += new EventHandler(context_Loaded);
            }
    
            private void context_Loaded(object sender, System.Windows.Ria.Data.LoadedDataEventArgs e)
            {
                Populate();
            }
    
            public void Populate()
            {
                this.ClearItems();
                foreach (UserStatus status in context.UserStatus)
                {
                    this.Add(status);
                }
            }
        }

     Then I added it to my <Application.Resources> in App.xaml. So fart it is working, but I have not done updates to the collection yet, for with I need to watch the context for that. Somehow it feels like a lot of this should be done for you, is there an easier way when the collection is coming from a domain context?

    Thanks!

  • RogerGu

    RogerGu

    Participant

    1124 Points

    420 Posts

    Re: Re: DataFormComboBoxField

    Jun 10, 2009 05:54 PM | LINK

    David, Bob

    Thanks, but I am still stuck.

    Bob, in your example I have the binbing with the converter working, but I cannot seem to set the itemsource of the DataFormComboBoxField. In my case, I load the context well before I get to the page that has the DataFormComboBoxField. Meaning I can't depend on the context loaded event, and I cannot figure out a point in the lifecycle of the page where I can set the DataFormComboBoxField.ItemsSource when the DataFormComboBoxField itself is not null. What would you do if your context was completely loaded before you went to a page/control that had the DataForm?

    David, your example worked great for the ItemsSource, and I got it working. However, I could never get the binding to work with the selected item. Can you share an example of how you do this?

    Thanks,
    Roger

     

  • bbakermai

    bbakermai

    Member

    230 Points

    151 Posts

    Re: Re: DataFormComboBoxField

    Jun 10, 2009 06:28 PM | LINK

    1) Your context (or Dictionary) has to be visible in the page where you're trying to set the combo box's ItemsSource

    2) Check the count of the object (or Dictionary if you've already done that earlier like I do) that you are trying to bind to; If it's > 0, you have items to bind.

    3) Find the control and then set up Binding using the code-behind approah (setting ItemsSource, Converter, etc.) as in the example.

    Everything necessary to do what you are trying to do is in the example with the dictionary converter. The context could be loaded in App.xaml.cs for all it cares, as long as you reference it correctly.

    If this has answered your question, please hit the Mark as Answered link. Thanks!

    Bob Baker
    MicroApplications, Inc.
    Orlando .Net Users' Group