This is a migrated thread and some comments may be shown as answers.

GridViewDataColumn with XAML-Image

4 Answers 497 Views
GridView
This is a migrated thread and some comments may be shown as answers.
NoRyb
Top achievements
Rank 1
NoRyb asked on 22 Mar 2010, 01:40 PM
Hello

I'm trying to do the following in my c# code:
            Binding binding = new Binding(); 
            binding.Path = new PropertyPath("Image"); 
            binding.Mode = BindingMode.OneWay; 
 
            GridViewDataColumn imgCol = new GridViewDataColumn(); 
            DataTemplate dataTempl = new DataTemplate(); 
            FrameworkElementFactory rectElement = new FrameworkElementFactory(typeof(Rectangle)); 
            rectElement.SetBinding(Rectangle.FillProperty, binding); 
            rectElement.SetValue(Rectangle.WidthProperty, 16.0); 
            rectElement.SetValue(Rectangle.HeightProperty, 16.0); 
            dataTempl.VisualTree = rectElement; 
            imgCol.CellTemplate = dataTempl; 
 
            forGrid.Columns.Insert(0, imgCol); 

Every Row-Object has an "Image"-property which contains a XAML-Vectorgraphic. My target is to create a column for the rowobject which shows the image. I was able to do this with the WPF-Toolkit-DataGrid and was wondering how it would work with the RadGridView.

Any suggestions?

Thank you
NoRyb

4 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 22 Mar 2010, 03:13 PM
Hello NoRyb,

Your code seems to use the right approach .However  to be sure everything is set up right  I would recommend to test the element representing the graphic externally to the grid . When you are sure it works , just place it in the Cell template and it should show the graphic  inside the cell
Of course the underlying business object should have the Image property containing a valid graphic.

In case you still have troubles , just send me your project and I will gladly make the fixes necessary for you.

Regards,
Pavel Pavlov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
NoRyb
Top achievements
Rank 1
answered on 22 Mar 2010, 03:42 PM
Hi Pavel

If I do the following in XAML it works perfectly:
 
            <telerik:RadGridView.Columns> 
                <telerik:GridViewDataColumn Header="TestIcon"
                    <telerik:GridViewColumn.CellTemplate> 
                        <DataTemplate> 
                            <Rectangle Width="16" 
                                       Height="16" 
                                       Fill="{Binding Image}"
                            </Rectangle> 
                        </DataTemplate> 
                    </telerik:GridViewColumn.CellTemplate> 
                </telerik:GridViewDataColumn> 
            </telerik:RadGridView.Columns> 

and instead of the binding I tried this (which is about what is to be bound):
 
            <telerik:RadGridView.Columns> 
                <telerik:GridViewDataColumn Header="TestIcon"
                    <telerik:GridViewColumn.CellTemplate> 
                        <DataTemplate> 
                            <Rectangle Width="16" 
                                       Height="16"
                                <Rectangle.Fill> 
                                    <DrawingBrush Stretch="Uniform" 
                                                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                                                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                        <DrawingBrush.Drawing> 
                                            <DrawingGroup> 
                                                <DrawingGroup.Children> 
                                                    <GeometryDrawing Brush="#FFF8F885" 
                                                                     Geometry="F1 M 0,15L 10.9792,5L 21.9375,15L 21.9375,32.0833L 0,32L 0,15 Z "
                                                        <GeometryDrawing.Pen> 
                                                            <Pen LineJoin="Round" 
                                                                 Brush="#FF000000" /> 
                                                        </GeometryDrawing.Pen> 
                                                    </GeometryDrawing> 
                                                    <GeometryDrawing Brush="#FFF8F885" 
                                                                     Geometry="F1 M 10.9792,5L 22.0208,0L 32,10L 21.9375,15L 10.9792,5 Z "
                                                        <GeometryDrawing.Pen> 
                                                            <Pen LineJoin="Round" 
                                                                 Brush="#FF000000" /> 
                                                        </GeometryDrawing.Pen> 
                                                    </GeometryDrawing> 
                                                    <GeometryDrawing Brush="#FFF8F885" 
                                                                     Geometry="F1 M 32,10.0833L 32,25L 21.9375,32.0833L 21.9375,15L 32,10.0833 Z "
                                                        <GeometryDrawing.Pen> 
                                                            <Pen LineJoin="Round" 
                                                                 Brush="#FF000000" /> 
                                                        </GeometryDrawing.Pen> 
                                                    </GeometryDrawing> 
                                                    <GeometryDrawing Brush="#FFFFFF00" 
                                                                     Geometry="F1 M 0,20.0417L 21.9375,20.0417L 21.9375,25L 0,25L 0,20.0417 Z "
                                                        <GeometryDrawing.Pen> 
                                                            <Pen LineJoin="Round" 
                                                                 Brush="#FF000000" /> 
                                                        </GeometryDrawing.Pen> 
                                                    </GeometryDrawing> 
                                                    <GeometryDrawing Brush="#FFFFFF00" 
                                                                     Geometry="F1 M 21.9375,20L 32,14.9583L 32,20.0833L 21.9375,25L 21.9375,20 Z "
                                                        <GeometryDrawing.Pen> 
                                                            <Pen LineJoin="Round" 
                                                                 Brush="#FF000000" /> 
                                                        </GeometryDrawing.Pen> 
                                                    </GeometryDrawing> 
                                                </DrawingGroup.Children> 
                                            </DrawingGroup> 
                                        </DrawingBrush.Drawing> 
                                    </DrawingBrush> 
                                </Rectangle.Fill> 
                            </Rectangle> 
                        </DataTemplate> 
                    </telerik:GridViewColumn.CellTemplate> 
                </telerik:GridViewDataColumn> 
            </telerik:RadGridView.Columns> 

So everything seems to be working but if I do what I wrote in my last Post in Code, and I execute it at the constructor of the Codebehind of the file where the RadGridView is in I get the Exception "FrameworkElementFactory must be in a sealed template for this operation" - InvalidOperationException. It seems to happen in "Telerik.Windows.Controls.GridViewColumn.CreateCellElement" and the Exception-StackTrace is:

   bei System.Windows.FrameworkElementFactory.InstantiateUnoptimizedTree() 
   bei System.Windows.FrameworkTemplate.LoadContent() 
   bei Telerik.Windows.Controls.GridViewColumn.CreateCellElement(GridViewCell cell, Object dataItem) 

Any idea what the problem is?

Thanks
NoRyb
0
Pavel Pavlov
Telerik team
answered on 23 Mar 2010, 01:49 PM
Hello NoRyb,

Please try calling the Seal() method of the DataTemplate before using it . This might actually help.

Greetings,
Pavel Pavlov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
NoRyb
Top achievements
Rank 1
answered on 23 Mar 2010, 02:02 PM
Hello Pavel,

Seal() did the trick :-)

Thank you
~NoRyb
Tags
GridView
Asked by
NoRyb
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
NoRyb
Top achievements
Rank 1
Share this question
or