Welcome to the Xceed Community | Help
Community Search  
More Search Options

Using ForeignKeyConfiguration to bind drop-down list on DataGrid cell to DataTable/Dictionary property

Sort Posts: Previous Next
  •  07-02-2009, 4:54 PM Post no. 22234

    Using ForeignKeyConfiguration to bind drop-down list on DataGrid cell to DataTable/Dictionary property

    This post contains three questions really, so I'll try and be as succinct as possible.

    I have a class (MyClass) derived from UserControl containing a DataGrid with a Column defined as thus:

    <xcdg:Column FieldName="Contact" ForeignKeyConfiguration="{StaticResource contactForeignKeyConfiguration}" />

    The ForeignKeyConfiguration is defined in my Resources as thus:

    <xcdg:ForeignKeyConfiguration x:Key="contactForeignKeyConfiguration"
          ItemsSource="{Binding Source={x:Static Application.Current}, Path=Contacts}"
          ValuePath="ContactID" />


    I then have the following property defined in my App class:

    public DataTable Contacts
    {
      get
      {
        DataTable dt = new DataTable();
        dt.Columns.Add("ContactID", typeof(int));
        dt.Columns.Add("Contact", typeof(string));
        DataRow dr = dt.NewRow();
        dr["ContactID"] = 1;
        dr["Contact"] = "Jason";
        dt.Rows.Add(dr);
        return dt;
      }
    }

    First question
    However, in a similar way to the 'Ship Via' column in the TableView example, I expected a drop-down list to appear on the cells of my DataGrid under the 'Contact' column showing one item "Jason".  Instead, it showed one item "System.Data.DataRowView".  This is the first question - how do I get it showing the value in the 'Contact' column of my DataTable?

    Second question
    Now, if I move the Contacts property into MyClass and change my ForeignKeyConfiguration to:

    <xcdg:ForeignKeyConfiguration x:Key="contactForeignKeyConfiguration"
          ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=Contacts}"
          ValuePath="ContactID" />

    ...this doesn't work.  The property isn't even read.  How do I achieve this so that the property in my UserControl class is correctly assigned to the ItemsSource property of my Column's ForeignKeyConfiguration?

    Third question
    And finally, I would like the property to be of type Dictionary<int,string> rather than DataTable.  Can I achieve the same auto-detection of drop-down items as with a DataTable?


    Thanks for any help that you can give!

    Jason


    Assistant Vice President, .NET Development
    Barclays Capital, UK
  •  07-03-2009, 11:43 AM Post no. 22255 in reply to 22234

    Re: Using ForeignKeyConfiguration to bind drop-down list on DataGrid cell to DataTable/Dictionary property

    I will answer the 3rd question since the first 2 are moot in the case where "3" is possible. That said, yes, you can use a Dictionary<int, string> rather than a DataTable; however, you will need to create a custom ForeignKeyConverter to provide the correct key for a value and vice versa. 

    Take a look at the  "Custom key/value mappings" example in the "Foreign Key Detection" topic. It demonstrates what you need to do.


    Technical Writer/Technical Support Team Manager - Xceed Software

    In three words I can sum up everything I've learned about life: it goes on.
  •  07-09-2009, 7:08 AM Post no. 22408 in reply to 22255

    Re: Using ForeignKeyConfiguration to bind drop-down list on DataGrid cell to DataTable/Dictionary property

    Thank you.  With your answer and the assistance you provided in this thread - http://xceed.com/CS/forums/thread/22376.aspx - I was able to come up with a solution in the code-behind.  A solution in the XAML wasn't viable for my purposes as I want to read the items into ItemsSource from an object (Dictionary) created late at run-time and isn't available in the visual tree.

        // Put all the contacts into a dictionary list
        Dictionary<int, string> contacts = new Dictionary<int, string>();
        foreach (ContactDetails contactDetails in _data.ClientAddressData)
        {
            contacts.Add(contactDetails.ID, contactDetails.ContactName);
        }

        // Update the drop-down list of the Contact Column to contain all the contacts available
        ForeignKeyConfiguration fkcContacts = new ForeignKeyConfiguration();
        fkcContacts.ForeignKeyConverter = new ContactForeignKeyConverter();
        fkcContacts.ItemsSource = contacts;
        fkcContacts.ValuePath = "Key";
        fkcContacts.DisplayMemberPath = "Value";
        ((Column)dgcAlertsAndCalls.Columns["Contact"]).ForeignKeyConfiguration = fkcContacts;


    And ContactForeignKeyConverter is defined as thus:

        public class ContactForeignKeyConverter : ForeignKeyConverter
        {
            public override object GetValueFromKey(object key, ForeignKeyConfiguration configuration)
            {
                if (key != null)
                {
                    Dictionary<int, string> contacts = configuration.ItemsSource as Dictionary<int, string>;
                    if (contacts != null)
                    {
                        foreach (KeyValuePair<int, string> contact in contacts)
                        {
                            if (contact.Key == (int)key)
                            {
                                return contact.Value;
                            }
                        }
                    }
                }
                return null;
            }
        }


    Assistant Vice President, .NET Development
    Barclays Capital, UK
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.