Cast error when drawing Subgraphs in MSAGL - c#

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.

Related

How do I set a button to delete its parent element in the button's constructor?

I am creating a code behind that creates a scrollable list of informational frames based on data from an API I set up. Each frame has a background color to indicate a status I'm monitoring as well as a button to open up an informational dialogue and a button to remove the frame from the list. This is the code I have right now:
Frame alert_frame = new Frame
{
BorderColor = Color.Black,
BackgroundColor = color,
StyleId = obj.EventKey,
Content = new StackLayout
{
Orientation = Xamarin.Forms.StackOrientation.Horizontal,
Padding = 0,
Margin = 0,
Children =
{
new Button
{
BackgroundColor=color,
VerticalOptions=LayoutOptions.FillAndExpand,
HorizontalOptions=LayoutOptions.FillAndExpand,
TextColor=Color.White,
FontSize=30,
FontAttributes=FontAttributes.Bold,
Text="Info",
Command=(System.Windows.Input.ICommand)DisplayAlert("Event Info",obj.Change,"Okay")
},
new Button
{
BackgroundColor=color,
VerticalOptions=LayoutOptions.FillAndExpand,
HorizontalOptions=LayoutOptions.FillAndExpand,
TextColor=Color.White,
FontSize=30,
FontAttributes=FontAttributes.Bold,
Text="Delete",
Command= //This is what I need help with
}
}
}
};
I have a function called Remove that returns the desired parent frame and that function works correctly, so the problem I'm facing now is figuring out how I can cast the command
alert_stack.Children.Remove(Remove(obj))
Into the proper type to be set to the command attribute. The most recent thing I tried was
new Command(() => alert_stack.Children.Remove(Remove(obj)))
But this returned an invalid cast error upon running it. Does anyone have any idea how this might be done? Would it be better to create a separate function to create the buttons and their OnClick attributes and return them as a list and have that set to the children attribute of the StackLayout?
I appreciate any help

Creating Binding from Code-Behind not working C# XAML

I have the following code that creates a binding in code-behind. However, it does not seem to work (when the text in PageMarginTextBox is changed, nothing happens, and when the app is loaded, the Padding of newPage is not set to the text of PageMarginTextBox). To make matters worse, no Exceptions are thrown at all. All elements have been defined earlier on.
Binding pageMarginBinding = new Binding
{
Source = PageMarginTextBox,
Path = new PropertyPath("Text"),
};
newPage.SetBinding(ContentControl.PaddingProperty, pageMarginBinding);
//PageMarginTextBox.Text determines the Padding of newPage
How can I fix this? Any solutions would be appreciated. Thanks!
You are trying to Bind PaddingProperty to text. Padding property is of type Thickness and Text property is String.
I am not sure whether you want to bind padding / text, just giving you an idea if you want to bind the Padding.
Binding pageMarginBinding = new Binding
{
Source = PageMarginTextBox,
Path = new PropertyPath("Padding"),
};
newPage.SetBinding(ContentControl.PaddingProperty, pageMarginBinding);
Your problem is because you are trying to assign a string to a Thickness. In XAML the compiler internally translates the string "0,0,2,2" to Thickness object. But in code behind you have to write the code for the conversion yourself.
ThicknessConverter myThicknessConverter = new ThicknessConverter();
PageThickness= (Thickness)myThicknessConverter.ConvertFromString(PageMarginTextBox.Text);
Then you have to bind this to your control. Again this is only half the solution. You need to wire this up with the Binding.
private Thickness _pageThickness;
public Thickness PageThickness
{
get
{
return _pageThickness;
}
set
{
_pageThickness = value;
NotifyPropertyChanged("PageThickness");
}
Then you probably can bind it in XAML

Exposing VisualOffsets property

I am making an application in C# WPF and need the location of an object that I added to a panel. While debugging I found the VisualOffset property of the object that gives me the relative position. I just can't get the value from the code.
What I would like to do is (though it is not possible):
var display = new Display(); // This is a class that inhereit UserControl
.
.
// At some point when display is added to a panel
var position = display.VisualOffset; // This property is not accessible
So how can I get the relative position of an object?
Use TranslatePoint method of the Display instance. Set the parent control as a target. The code below will give you coordinates of display on its parent. If the container is further down the visual tree then you have to find a parent of a parent.
In my sample I'm finding that on the parent. I'm doing that on button click and then return the results as a string to a text box - purely for simplicity sake. But the idea is the same wherever you use it:
private void Button_Click(object sender, RoutedEventArgs e)
{
var parent = display.Parent as UIElement;
var location = display.TranslatePoint(new Point(0, 0), parent);
this.myTextBox.Text = $"x: {location.X}, y: {location.Y}";
}
display is of course an instance of the Display user control.

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

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

Using image transparency between Picture Boxes stores as Arrays

I am using c# and winForms to create an application that allows me to have 6 Picture Boxes (each with a different image) on top of one another. Each image has alot of white space so i am trying to use transparency to allow the user to toggle images on and off. When an image is toggled off, the parent/child of all the other images will be updated to allow the transparency to work.
I First created a custom class:
public class MyImageWrapper
{
public MyImageWrapper Parent { get; set; }
public PictureBox PictureBox { get; set; }
public MyImageWrapper(PictureBox i, MyImageWrapper parent = null)
{
Parent = parent;
PictureBox = i;
}
}
Then I Created the Array:
MyImageWrapper[] pictureBoxArray = new MyImageWrapper[6];
This next code is a test to Check if transparency works:
pictureBox[0] = new MyImageWrapper(pictureBox1);
pictureBox[1] = new MyImageWrapper(pictureBox2);
pictureBox1.Image = (Image.FromFile(""+ Application.StartupPath +"../../../images/imageA.png"));
pictureBox2.Image = (Image.FromFile(""+ Application.StartupPath +"../../../images/imageB.png"));
pictureBoxArray[1].Parent = pictureBoxArray[0];
Unfortunately I do not receive any error messages so it seems to be working. However when I run the program Picture Box 2 is not a child of picture box 1 (and thus transparency does not work)
I have not gotten as far as setting the images to be turned on or off yet, I am still trying to sort out the transparency.
The problem specifically is this line:
pictureBoxArray[1].Parent = pictureBoxArray[0];
When testing I replaced the above line with:
pictureBox2.Parent = pictureBox1;
This does exactly the same thing (but works!). However the way my program needs to work later I cannot do it like this. I need to fix the Array version.
Any help would be greatly appreciated.
Wouldn't you need to put:
pictureBoxArray[1].PictureBox.Parent = pictureBoxArray[0].PictureBox
?
The two lines:
pictureBoxArray[1].Parent = pictureBoxArray[0];
pictureBox2.Parent = pictureBox1;
You have put are not equivalent. In one you are assigning the parent of the ImageWrapper and in the other you are assigning the parent of the PictureBox.
I don't know if this is an error in how you have presented the code or in your program itself.

Categories

Resources