views:

30

answers:

2

Hi !

I use a listview to display data like a data matrix (columns and rows). My problem is : my items are typed : MatrixCellVM. I tried everything I found on the net to apply a DataTemplate on this items but nothing worked.

Here is the latest technique I'm using

foreach (var col in dataMatrix.Columns)
        {
            //create the data template
            DataTemplate cellLayout = new DataTemplate();
            cellLayout.DataType = typeof(MatrixCellVM);

            //set up the stack panel
            FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Grid));
            spFactory.Name = "myComboFactory";

            //set up value textblock
            FrameworkElementFactory cellValue = new FrameworkElementFactory(typeof(TextBlock));
            cellValue.SetBinding(TextBlock.TextProperty, new Binding("Value"));
            cellValue.SetValue(TextBlock.ToolTipProperty, "Value");
            spFactory.AppendChild(cellValue);

            //set the visual tree of the data template
            cellLayout.VisualTree = spFactory;

            gridView.Columns.Add(
                new GridViewColumn
                    {
                        Header = col.Name,
                        DisplayMemberBinding = new Binding(string.Format("[{0}]", count)),
                        CellTemplate = cellLayout
                    });
            count++;
        }

One more thing, when I override ToString() in MatrixCellVM, the value is displayed (but with this technique I add a context menu or give any color to my value).

Thanks for your help !

Toast.

A: 

I solved the problem but have another one.

So what I did is pretty simple. DisplayMemberBinding and CellTemplate can't be put together.

So I deleted the whole c# code creating the datatemplate.

The only thing that remains is :

gridView.Columns.Add(
                new GridViewColumn
                    {
                        Header = col.Name,
                        CellTemplate = (DataTemplate)listView.Resources["MatrixCellVMTemplate"]
                    });

Thus, I added in my xaml code defining the datatemplate.

My new problem is that my rows are arrays of MatrixCellVM (as it is a matrix, I don't have properties and I don't know by advance how many columns I will have).

That was the point of the DisplayMemberBinding = new Binding(string.Format("[{0}]",count)) code in my top post.

How could I reproduce this behavior in my datatemplate (what is the binding in my textblock).

If anything is not clear, let me know.

Thanks

Toast
A: 

Does anyone have an idea ?

Toast