views:

606

answers:

2

Trying to bind to a collection in WPF, I got the following to work:

XAML:

<toolkit:DataGrid Name="dgPeoples"/>

CS:

namespace DataGrid
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1
    {
        private readonly ObservableCollection<Person> personList = new ObservableCollection<Person>();

        public Window1()
        {
            InitializeComponent();

            personList.Add(new Person("George", "Jung"));
            personList.Add(new Person("Jim", "Jefferson"));
            personList.Add(new Person("Amy", "Smith"));

            dgPeoples.ItemsSource = personList;
        }
    }
}

unnessecary probably but here is Person class:

namespace DataGrid
{
    public class Person
    {
        public string fName { get; set; }
        public string lName { get; set; }

        public Person(string firstName, string lastName)
        {
            fName = firstName;
            lName = lastName;
        }
    }
}

But what I really need is this in DataGridComboBoxColumn 's. Here are my revisions:

XAML:

<toolkit:DataGrid Name="dgPeoples" Grid.Row="0" AutoGenerateColumns="False">
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridComboBoxColumn Width="5*"/>
        <toolkit:DataGridComboBoxColumn Width="5*"/>
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

C#:

Stays same.

Problem now is that I get empty combobox columns! Any ideas how I can get this to work?

In the long run I need 2 way binding, where a double click on the firstname column brings up the comobo box which then holds the options of all possible first names in the collection (i.e. George, Jim and Amy).

Grateful any assistance!

A: 
Metro Smurf
This doesn't work. I get the same as before, except obviously they now have headers. It is the correct number of rows, but blank, and double clicking brings up the comobobox, but this is also blank.
baron
See edit for reference to other SO post which should address the problem.
Metro Smurf
Can you help show me how I would adapt my problem to the solution in that SO question? Because I have set ItemsSource in code behind, he does it in xaml
baron
A: 

I just wanted to point you to one of the best reference when it comes to the WPF Toolkit Datagrid, it comes from from Samuel Moura I downloaded the 15 samples and keep them handy,I'm giving you the link because you'll find the Datagrid post series more useful than any reference than I can give you as quick answer

Enrique G
@Enrique: Your XamlWriter question was deleted before I could answer it. Presumably you found that the problem was that Concept (obviously a data or ViewModel object) had a pointer to the UserControl, so you ended up recursively trying to serialze a cycle. I was going to mention this but I also wanted to mention that the fact that you had a "ConceptAnswerUserControl" field in your "Concept" object tells me your data and UI layers are poorly separated. Your data (or ViewModel) should never have a pointer to a UIElement of any kind. This leads to all kinds of problems. (continued...)
Ray Burns
(continued) For example, what if you wanted to display and edit the same Concept in two places - there would then be two UserControls for your Concept. I strongly recommend you go back and rethink how you have architected your data layer (or view model) and the UI binding to it. Figure out how to keep the data layer from ever referencing the UI at all, and you will have a better design.
Ray Burns
@Ray Burns Actually I still having the problem, Concept is pure data and, DOES NOT contain any reference to the UI; in ConceptAnswerUserControl we want to store some sort of metadata that tells the client side how the visual rendering of "concept" should behave, when creating a "concept" the author (business people) can choose how to render it, for example ConceptAnswerUserControl.ConceptAnswerType could be a "YesNo" or "Text_Single" "Text_Multiple", anyway, everything still under development and exploring approaches one of them -WPF rendering- is where I'm having this error.
Enrique G
Ray Burns