Element is already the child of another element, Windows Phone 8 - c#

Hey embarrassingly enough I ran into the error stated in the title. As many others have sought to find a solution online, but I was not able to do so.
Okay so my issue, is that I have an ObservableCollection of my elements which is used in an Itemscontrol. I want to create a new element of one of the elements in the ObservableCollection. Because of the trouble with working with ObservableCollection I created a serializing and Deserializing of the specific object. like this>
IsolatedStorage.SerializeSElement("saveString", saveElement);
IsolatedStorage.DeSerializeSElement("saveString", loadElement);
Which I than hoped gave me a completely new element. But I still have the same element. How do I solve the issue if say my class looks like>
public class myElement(){
int posx;
int posy;
double id;
bool isMoveable;
}
But there is still the error, is there way to solve this error?
Code
private Geometry createGeometry(SViewModel sModelRec)
{//TEST
sModel = sModelRec.Gear;
sModelRec = null;
//new Path.Combine(DecorationOnShield[i].Gear.Path,"");
PathGeometry pathGeometry = new PathGeometry();
PathFigure testPathFigure = new PathFigure();
System.Windows.Shapes.Path pathTesting = new System.Windows.Shapes.Path();
var b = new System.Windows.Data.Binding
{
Source = sModel.Path
};
System.Windows.Data.BindingOperations.SetBinding(pathTesting, System.Windows.Shapes.Path.DataProperty, b);
pathTesting.Width = sModel.Width;
pathTesting.Height = sModel.Height;
Geometry geometry = pathTesting.Data;
return geometry;
}
So when I return the element and add it to the GeometryGroup I get the error.

You are trying to Add a Control and that Control already exists in your VisualTree.
The solution is simple
You have Source Parent Container = The control in witch resides the element you want to add to the other container
Target Parent Container = The control in witch you want to add your element.
To solve your problem just remove the element from the Source Parent Container and then add it to your Target Parent Container
Note: to have an answer more specific to your situation you must post your view or the lines where the error is getting thrown

Related

Cast error when drawing Subgraphs in MSAGL

I have a root Subgraph object with four child Subgraphs each containing a few Nodes. I have added all the Subgraphs and Nodes to the Graph and set the RootSubgraph on the Graph. However, when trying to draw the Graph I get a cast error from within the library (Microsoft.Msagl.GraphViewerGdi.Draw.cs, line 1044) where the GeometryNode of a Subgraph is cast to a Cluster. The GeometryNode is set to a Cluster in GeometryGraphCreator.ProcessSubgraphs and the error message gives me no information about why the cast fails. How can I fix this?
For now I've simply set the Label to null to avoid the drawing code but I would like to use a label eventually.
The problem was in my initialisation of the Subgraph:
var subgraph = new Subgraph(id)
{
Attr =
{
Id = id,
FillColor = fillColor,
},
Label =
{
FontSize = 10,
Text = id
}
};
Assigning the Label property this way skipped some extra set up code needed to correctly link the Label with the Subgraph. Leaving it out altogether lets the constructor handle the Label itself.

How to databind ViewModels objects as 3DModels in WPF

I am working on an application that is generating a bunch of collections of ViewModel Instances representing wooden planks (has all needed attriuttes-x,y,z,posx,posy,posz). This works fine.
Now I would like to visualize these Planks inside of the app in a 3d environments:
I have found plenty of examples how to creates boxes with viewport3d Frameworkelement, but my problem is that all of them show how to statically define a single 3dobject.
I was trying and experimenting, but I did not manage to find a single example of how to databind a whole collection, transform single boxes, rotate, and resize them.
Does anyone know how to databind 3D viewmodel collections in WPF?
You can
Put to your Window a dependent property ViewModels and on changing it create your geometry models with bindings in code behind
or
Create a UserControl based ex. on Viewport3D and do 1. for it
The bindings you can create such way(pseudo code):
var geo = new MeshGeometry3D { Positions = new Point3DCollection(pointsLists), TriangleIndices = new Int32Collection(indexes) };
geo.Freeze();
var mat = new DiffuseMaterial(Brushes.Gray); mat.Freeze();
var bMat = new DiffuseMaterial(Brushes.Red); bMat.Freeze();
var geomod = new GeometryModel3D(geo, mat);
geomod.BackMaterial = bMat;
geomod.Transform = new ScaleTransform3D();
var bndng = new Binding("ScaleValue");
bndng.Source = SomeViewModel;//Here put the propriate viewmodel
BindingOperations.SetBinding(geomod.Transform, ScaleTransform3D.ScaleXProperty, bndng);
BindingOperations.SetBinding(geomod.Transform, ScaleTransform3D.ScaleYProperty, bndng);
BindingOperations.SetBinding(geomod.Transform, ScaleTransform3D.ScaleZProperty, bndng);
geomod.Geometry = geo;
Model3DGroup.Children.Add(geomod);//Here you have to find reference to you Model3DGroup
You can use an example that you can copy/paste from:
Same ScaleTransform3D for different geometries

Can't find controls that don't have enough defined properties in Coded UI

This may be a noobish question, but in my records in Coded UI Tests, I have recorded a lot of controls that don't have enough defined properties to be found in playback.
For exemple:
public HtmlEdit UIItemEdit
{
get
{
if ((this.mUIItemEdit == null))
{
this.mUIItemEdit = new HtmlEdit(this);
#region Search Criteria
this.mUIItemEdit.SearchProperties[HtmlEdit.PropertyNames.Id] = null;
this.mUIItemEdit.SearchProperties[HtmlEdit.PropertyNames.Name] = null;
this.mUIItemEdit.SearchProperties[HtmlEdit.PropertyNames.LabeledBy] = null;
this.mUIItemEdit.SearchProperties[HtmlEdit.PropertyNames.Type] = "SINGLELINE";
this.mUIItemEdit.FilterProperties[HtmlEdit.PropertyNames.Title] = null;
this.mUIItemEdit.FilterProperties[HtmlEdit.PropertyNames.Class] = null;
this.mUIItemEdit.FilterProperties[HtmlEdit.PropertyNames.ControlDefinition] = "type=\"text\" value=\"\"";
this.mUIItemEdit.FilterProperties[HtmlEdit.PropertyNames.TagInstance] = "5";
this.mUIItemEdit.WindowTitles.Add("http://cms.home.psafe.com/");
#endregion
}
return this.mUIItemEdit;
}
In this post, I learned about SearchProperties, but it doesn't look to be an appropriate solution in this case.
Is there any other way to wrap these controls properly?
You might be able to find it if its containing element can be found. You can use the containing element to scope the search. So, find that element's parent, then find an input type=text within it:
var container = new HtmlControl(bw); //where bw is the browser window
HtmlDiv parentDiv = new HtmlDiv(container);
parentDiv.SearchProperties[HtmlDiv.PropertyNames.Id] = "theIdOfYourDiv";
HtmlEdit edt = new HtmlEdit(parentDiv); //the search scope is narrowed down to the div only. This may be enough to find your control with the search property.
edt.SearchProperties[HtmlEdit.PropertyNames.Type] = "SINGLELINE";
You have two options:
Try crowcoder's solution of searching in the parent. The problem with this solution is when you move a control around you're going to be changing code a lot.
Add an Id property to all your controls in the HTML, this will make your Coded UI more robust and responsive to changes in the UI.

TreeListControl within each NavbarControl Group

I'm very new to WPF and I'm attempting to create a treelist navigation within each navbar group. Because the number of navbar groups and treelists are dynamic I have to make them in code rather than them be pre-defined in XAML.
I have tested the following so far which is meant to define the navbar group's content rather than use the default item
private void CreateGroup2(NavBarControl navBar)
{
NavBarGroup group2 = new NavBarGroup();
group2.Header = "Custom Content";
//Specify that the group's content should be defined via the Content property
group2.DisplaySource = DisplaySource.Content;
TreeListControl tree = new TreeListControl();
tree.ItemsSource = TreeList_DataBinding.Stuff.GetStuff();
group2.Content = tree;
navBar.Groups.Add(group2);
}
This gives an Exception: Grid.InfiniteGridSizeException: By default, an infinite grid height is not allowed since all grid rows will be rendered and hence the grid will work very slowly. To fix this issue, you should place the grid into a container that will give a finite height to the grid, or you should manually specify the grid's Height or MaxHeight. Note that you can also avoid this exception by setting the TreeListControl.AllowInfiniteGridSize static property to True, but in that case the grid will run slowly.
I'm a little confused as I'm not using a grid? Can anyone give any pointers what's wrong and how I can add a treview under each navbar group?
Thank You
It feels a bit wrong answering my own question but I managed to get it working using the following
private void CreateGroup2(NavBarGroup navBarGroup)
{
System.Windows.Controls.TreeView treeview = new System.Windows.Controls.TreeView();
TreeViewItem nod = new TreeViewItem();
nod.Header = "Tree Node1";
treeview.Items.Add(nod);
TreeViewItem nod1 = new TreeViewItem();
nod1.Header = "Tree Node2";
treeview.Items.Add(nod1);
TreeViewItem nod2 = new TreeViewItem();
nod2.Header = "Tree Node3";
nod1.Items.Add(nod2);
//StackPanel stcPnl = new StackPanel(); /optiona
//stcPnl.Children.Add(treeview);
//navBarGroup.Content = stcPnl;
navBarGroup.Content = treeview;
navBarGroup.DisplaySource = DisplaySource.Content;
}

Image index of TreeView node changes upon selection

When I tried using the imagelist in treeview, the image index changes when treenode is clicked. I have no idea why it is happening. Can anyone help me?
Thanks in advance
You need to set both the ImageIndex and the SelectedImageIndex on the tree node.
'SelectedImageIndex's intent is to allow displaying a different image upon selection than what is set by the 'ImageIndex' for a particular node. To keep these two consistent it is necessary to set them to the same value. This can be done at design time or programmatically depending on your needs.
For example, if the images never change then it is as simple as setting them concurrently when a new node is added to the TreeView:
int myCurrentImageIndex = 0;
TreeNode node = myTreeView.Nodes.Add("new node!");
node.ImageIndex = node.SelectedImageIndex = myCurrentImageIndex;
However, if you do change the ImageIndex value for any reason after its initial creation (such as a response to some kind of user action), then you must also change the SelectedImageIndex as well. Otherwise, they will become inconsistent.
int myNewImageIndex = 1;
node.ImageIndex = node.SelectedImageIndex = myNewImageIndex;
(Note it is not enough to set them to be the same in the event handler of the 'AfterSelect' event. It must be done anywhere in your code where ImageIndex changes.)
you can directly do it in the constructor :
TreeNode node = new TreeNode("My treenode", 1, 1);
TreeNode tn = new TreeNode();
tn.Text = "NewRecord";
tn.ImageIndex = 1;
treeView.SelectedNode.Nodes.Add(tn);
treeView.SelectedNode = tn;
treeView.SelectedNode.SelectedImageIndex = tn.ImageIndex; // <--- Problem solved
tn.BeginEdit();
Just Add this line:
Node.SelectedIndex:=Node.ImageIndex;

Categories

Resources