Hi,
I'm trying to create a TableShape project that will visualize tables from our DB, and will enable the users to open tables and create joins between row in a different tables.
I used your demo's code for the TableShape, but instead of --> SamplesFactory (class) --> LoadSample (method) --> diagram.Load(xml) (line 146), I created a TableShape and added it by using : diagram.AddShape(tableShape).
Now I'm trying to add rows to the table, but I can't use the RowShape you have in the demo, I get an exception that the rowShape not suitable for the tableShape.
How can I solve it? How can I add rows to my table?
Best Regards.
5 Answers, 1 is accepted
We managed to reproduce the exception that you described. Basically, the TableShape example is created to work in data binding scenario. There is no built-in logic that handles the scenario when you work directly with the visual elements (TableShape and RowShape). You are getting this type of exception because the wrong Style is applied in the ShapeStyleSelector. To work this around, you can update the implementation in the SelectStyle() method in the selector class so that it returns the correct style.
However, working directly with the custom shapes will require customization on several places in the code. This is why we recommend you to work with the view models (RowModel and TableModel) of the shapes and the graph source of the diagram. In addition, you can take DataBinding help article.
Hope this information is helpful.
Regards,
Dinko
Telerik
I am trying to do something similar, I wanted to iterate through a Query and add the rows based on the Table Information. So I used your models and from the tableShapes Demo to try to get a good starting point. I have the style Selector, and I used this code here
to add a shape with a single row in it.
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
TableModel table = new TableModel();
tgs.AddNode(table);
var itemToAdd = new RowModel() { ColumnName = "NewRow", DataType = DataType.String };
table.AddItem(itemToAdd);
}
I get error
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: 'RowShape' TargetType does not match type of element 'RadDiagramShape'.
Which is odd since it is derived from RadDiagramShapeBase
I have tried adding the Shapes before adding the container to the viewmodel, no bueno. If comment out the row addition in the table model, I get the TableShape.
DataContext does nothing for me here
Ok. so I decided to change directions and scrap the Table Shape Example, because all I really needed was the rowShape. I am able to add Row Shapes to a stock ContainerShape.
I decided to use the RowShape Style (Would rather not use a style, styles suck) and set that programmatically;
first I added a TableInfo class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RoundPegTest
{
class TableInfo
{
public string ColumnName { get; private set; }
public string DataType { get; private set; }
public TableInfo(string columnName, string dataType) {
this.ColumnName = columnName;
this.DataType = dataType;
}
}
}
//after filling the Info class and setting each one to a list i iterate
foreach (TableInfo info in columnlist) { //iterate through each table
RadDiagramShape shape = new RadDiagramShape(); //create a shape
shape.Style = (Style)FindResource("rowStyle"); //select the rowStyle from TableShape Example
Debug.WriteLine(info.ColumnName); // just making sure I have data inside the class and I do
shape.DataContext = info; //Info has two variables get; set; ColumnName DataType, and rowStyle Text boxes in the // datatemplate are bound to ColumnName and DataType So I set the Data context here, and no Bueno
shape.Position = new Point(0, cont.Items.Count * 30); // just positioning each Row;
cont.Items.Add(shape); //adding to the shape
}
Ok So the Code works as expected with the exception being that the binding doesn't seem to want to work. So I get Blank row shapes instead of the ColumnName and DataType that I am expecting. I could just set the text on each one manually, but I havent the foggiest of how to get the TextBlock and set its text from Code Behind.
What I would rather have is a shape where I could Throw two named TextBlocks that are named and be able to set them in code behind. Starting to wonder if this WPF stuff is worth the trouble really. I am old school, I like to just create stuff with code. XML is for data. Anyway, any help figuring out why setting the data context of the shape does absolutely nothing for me
I need more time to check your scenario. I will contact you as soon we have more information about the case.
Regards,
Dinko
Telerik
Let me start first with that the RadDiagramShape is ContentControl. This means that the bindings in its ContentTemplate point to the Content property instead of the DataContext. In other words, to set the context of the RadDiagramShape, you need to set its Content property.
RadDiagramShape shape =
new
RadDiagramShape();
shape.Style = (Style)FindResource(
"rowStyle"
);
shape.Content = yourBusinessObject;
. . .
Regards,
Dinko
Telerik