Silverlight 4: How to access control created from codebehind - silverlight

I'm trying to create a method to draw a Line(path) between two UserControls. I found a post by someone that gave me a general pointer on how to do this, I implemented the code succesfully and started adapting it to my needs.
I am having a problem with accessing the user control:
Button b2 = new Button();
var transform2 = b2.TransformToVisual(b2.Parent as UIElement);
Works as it should, but my buttons get created dynamicaly through a method so I can't access them as "b2".
I tried the following:
var transfrom3 = canvas1.Children[0].TransformToVisual(canvas1.Children[0].Parent as UIElement);
but accessing it like that gives me an error on .Parent.
If have also tried:
var p1 = this.FindName(ps.ProcessID.ToString());
var p2 = this.FindName(ps.PreID.ToString());
////get geo data from both controls
var transform1 = p1.TransformToVisual(p1.Parent as UIElement);
var transform2 = p2.TransformToVisual(p2.Parent as UIElement);
Can anyone tell me how i can access these UserControls?

This line ought to work:-
var transfrom3 = canvas1.Children[0].TransformToVisual(canvas1.Children[0].Parent as FrameworkElement);
as you have discovered UIElement doesn't have Parent, its the FrameworkElement that adds the Parent property.
However it would be better to access the control via its name (I'll assume you are assigning a Name to the dynamically added controls).
This:-
var p1 = this.FindName(ps.ProcessID.ToString());
ought to work assuming this is UserControl and the FrameworkElement in question was given the matching name in code:-
canvas1.Children.Add(new Button() {Name = ps.ProcessID.ToString(), Content="Hello"});

Hmm I did not see that you changed UIElement to FrameworkElement, this setup works for me now:
var control1 = this.FindName(ps.ProcessID.ToString()) as FrameworkElement;
var control2 = this.FindName(ps.PreID.ToString()) as FrameworkElement;
var transform1 = control1.TransformToVisual(control1.Parent as UIElement);
var transform2 = control2.TransformToVisual(control2.Parent as UIElement);
If I add the control to a var as a FrameworkElement, then I can use the .Parent on the control again.
Thanks for your answer Anthony!

Related

Get WPF TabItem container from UserControl

I am setting the content of a UserControl, SIASystemTab, to a TabItem, ConfigTab. How do you access the TabItem from the UserControl using code behind? I would like to reuse the methods in the TabItem from multiple UserControls.
var subSystem = new SIASystemTab(opCo);
var configTab = new ConfigTab()
{
Header = "New Header*",
Content = subSystem
};
One simple way is to create a property in SIASystemTab like this
public ConfigTab myConfigTab {get; set;}
Then assign the ConfigTab instance to this property
subSystem.myConfigTab = configTab;
Later on u can use this as per your need
Cast the Content property of the UserControl to ConfigTab:
var tab = this.Content as ConfigTab;
var siaSystemTab = tab.Content;

c# WPF Automate GridViewColumnHeader Click event

Is there a possibility to call the Click-Event of a ListViewColumnHeader programatically?
I try to write an integration test about sortable columns in a ListView with a GridView and want to do something like this:
var list = new ListView();
var grid = new GridView();
var column = new GridViewColumn();
var header = new GridViewColumnHeader();
column.Header = header;
grid.Columns.Add(column);
header.DoClick(); // <-- not possible directly - what can I do
I already tried to achieve the goal by using the GridViewColumnHeaderAutomationPeer but did not succeed.
It is possible to Raise the Click event with the following line of code.
list.RaiseEvent(new RoutedEventArgs(GridViewColumnHeader.ClickEvent, header));

transforming the Clip of a UIElement

I am using C#, Silverlight, Visual Studio for Windows Phone 7.
I would like to get the Clip of a UIElement that includes any transform that has been done on the UIElement.
This example gives me the Clip but does not include any transform from the UIElement:
// uie is a UIElement taken from a PhoneApplicationPage
Geometry myClip = uie.Clip;
I tried the following, but it ended up moving the UIElement:
var frame = Application.Current.RootVisual as PhoneApplicationFrame;
var page = frame.Content as PhoneApplicationPage;
GeneralTransform gt = uie.TransformToVisual(page);
uie.RenderTransform = gt as Transform;
Geometry myClip = uie.Clip;
I also tried to add an inverse transform at the end to undo any movement, but that seemed to make it worse:
uie.RenderTransform = gt.Inverse as Transform;
Please help me get the Clip with the original transform of the UIElement without messing with the UIElement itself.
Thanks in advance.
I figured out how to solve this without messing with the original UIElement. I needed to create a new Geometry object with the same data rather than using the original Geometry. Not only does this separate the new data from the old data, it also prevents an object from possibly getting assigned as a child to multiple objects.
Here is the resulting code I used:
var frame = Application.Current.RootVisual as PhoneApplicationFrame;
var page = frame.Content as PhoneApplicationPage;
GeneralTransform gt = uie.TransformToVisual(page);
var place = gt.Transform(new Point());
// declaring new variables to hold these values
Geometry geom;
Path path = new Path();
// here I checked for other restrictions on my UIElements before assigning the variables
geom = new RectangleGeometry();
(geom as RectangleGeometry).Rect = new Rect(place.X, place.Y, uie.RenderSize.Width, uie.RenderSize.Height);
path.Data = geom;
I mentioned that I check for some restrictions on my UIElements before assigning the variables. This is related to another question I posted about clips (here).

Silverlight 4, SetBinding not working

I want to bind the contents of a HyperlinkButton to a resource programmatically, it't not working. This is the code I have so far:
HyperlinkButton Link1 = new HyperlinkButton();
Link1.Style = Application.Current.Resources["LinkStyle"] as Style;
Link1.NavigateUri = new Uri("/Home", UriKind.Relative);
Link1.TargetName = "ContentFrame";
Binding b = new Binding("TabTitles.HomePageTitle");
b.Source = this.Resources["ResourceWrapper"];
Link1.SetBinding(HyperlinkButton.ContentProperty, b);
I get a MethodAccessException
The MethodAccessException is commonly thrown when the public access modifier is missing from a member you want to access. Have you tested the TabTitles property of whatever is held in the "ResourceWrapper" resource? Have the then tested the HomePageTitle property of whatever TablTitles returns?
Note also that if HomePageTitle returns a UIElement you can only place that value once in the visual tree, however my guess is its a string.

Reset control's parent in WPF

If I add a control to a canvas and than removes it, I can not re-add it to the same canvas (or to any other canvas for that matter) any idea how can I reset the parent?
mainCanvas.Children.Add(item);
mainCanvas.Children.Remove(item);
mainCanvas.Children.Add(item); // Will throw an exception that parent was already set.
Thanks,
Eden.
Are you sure there is not something else going on in your code?
I just tried a new wpf application with :
public MainWindow()
{
InitializeComponent();
Button b = new Button();
b.Content = "hello";
Canvas c = new Canvas();
c.Children.Add(b);
c.Children.Remove(b);
c.Children.Add(b);
Content = c;
}
and it worked fine. Have you got any collection changed delegates?

Resources