Creating Silverlight UserControl - silverlight

I'm creating a silverlight user control that I should be able to drag and drop via blend. But this control needs to accept a map that is already on the page.
For eg.
Main.xaml contains a map control.
MapEditor.xaml contains buttons and other controls. In the .cs file, it needs to access a map control (the one in Main.xaml).
How do I go about getting this done?
I was thinking about adding a parameter in the contructor for MapEditor but how would I pass in the map as a parameter in design mode?
Thanks.
ps. I'm going to break out this control into a silverlight library so it could be used in multiple projects later.

You don't want to be giving your control a parameterised constructor, XAML will only construct types using their default constructor.
Simple Approach
The easiest approach would be to add DependencyProperty to your control to which you would assign the Map control (I'll use the type name MyMap in this example):-
public MyMap Map
{
get { return (MyMap)GetValue(MapProperty); }
set { SetValue(MapProperty, value); }
}
public static DependencyPropery MapProperty = new DependencyProperty("Map",
typeof(MyMap), typeof(MapEditor), new PropertyMetaData(null));
Now in Blend the Map property will appear in the Miscellaneous category in the Properties tab. You can then use the "Element Property" tab of the "Create Data Binding" to select the Map control to which it should bind.
Hard Core Approach
That said I would be inclined to build a proper customisable control following these guidelines Creating a New Control by Creating a ControlTemplate. With the addition that I would extend the ContentControl base class and include a ContentPresenter at the heart of the template. The control would make the assumption that the child control is a MyMap control.
This approach allows the entire appearance of the MapEditor control to be styled in Blend and it allows the Map control that is to be "edited" to be drap-drop onto the MapEditor as a child control.

Related

Frame Navigation in Wpf application

I have this application where there is a frame and I navigated the frame to a different Page where there are certain button which must be able to navigate the frame to another page but i don't know how to change the navigate property of the frame from another class somewhere. Can anyone help me out with that and also how do i change the text property of the page identifier from the another class.
Could you please try this? On button click event handler.
framename.Navigate(new Page1());
where Page1 is a xaml file to which you need to navigate to.
EDIT :
Your requirement could be easily implemented using MVVM model. The basic concept behind MVVM is that you can bind your data to properties in the view model class. So that you could easily control the properties of child classes from parent class and vice versa using instances of view model classes.
Could you try this for the time being?
((MainWindow)System.Windows.Application.Current.MainWindow).Framename.Navigate(new Page());
Also try this, to make the component as public.
For access controls in other WPF windows, you have to declare that control as a public, the default declaration of controls in WPF is public, and you can specify it with this code:
<TextBox x:Name="textBox1" x:FieldModifier="public" />
And after than you can search in all active windows in application to find than windows have that control like this:
foreach (Window window in Application.Current.Windows)
{
if (window.GetType() == typeof(Window1))
{
(window as Window1).textBox1.Text = "I change it from another windows";
}
}

Trouble with finding Automation Id for Border inside ItemTemplate

I have DataTemplate for a class which follows following hierarchy
I have given AutomationId to each of these Controls.
when I try to detect the highlighted border using Coded UI Test builder, i am unable to find it. whereas i am directly getting Checkbox inside one of its child control.
I am not able to automation to this parent control(Border) due to this problem.
If I place GroupBox instead of Border I am able to get this control.
posted actual datatemplate Here
please help out.
You should be able to use the Coded UI Test Builder's cross hairs to get to the level. Just point to it with the cross hairs, and when it points you to the checkbox, use the arrows to navigate the tree.
Another suggestion would be to use C# to manually add it to your map. This would be done by specifying an identifying property for the control. However, I think you may have trouble using AutomationProperties.Name or AutomationProperties.AutomationId. You'd have more luck adding an the Name property to the border and identifying the control with that.
So:
public HtmlControl BorderOrangeBorder
{
get
{
HtmlControl target = new HtmlControl([browser]);
target.SearchProperties["name"] = "OrangeBorder";
return target;
}
}

C# & WPF : How to update an in-code declared textbox height on textbox content change?

I'm fairly new to C#, I'm on a C# & Wpf course.
Here my teacher has told us to inherit from UserControls and create a wpf control library of our own. In order for us to create a simple UML Editor, and right now I'm working on a Class Control.
The visual representation of a class as seen here: http://www.softwarefactories.com/ScreenShots/CD-1.JPG
Now, to make things easy for the end user and me as the developer, the fields contained
in the class control contain editable textboxes. The fields can dynamically be added to the
control, for instance if the class doesn't have any member variables, it won't need a "property" field, and if the user would like to add a "description", he/she may do so easily.
That part is done, but right now the TextBoxes don't adapt in size when edited;
the user can add a line, but it won't resize the textbox.
What I want to do is to create an eventhandler of some sort, and have a function
run whenever the user changes the content of the textbox.
-How is this done?
Yes, you were absolutely right it's just that I handled the layout wrong further up in the
code. Anyway, it uses only a constant set of textboxes, so i just defined those in xml and
set the height to auto like it should be. In a stackpanel, they align neatly on top of oneanother. Thanks for your help, dnr3!

How do I replace a ControlTemplate part at runtime, in Silverlight 3?

I'm writing a custom control which uses a template defined in the resources section of my XAML and I'd like to replace one or more template parts in an instance of my control at runtime.
For example, if I have a part named "ActivePart" in the control's template, how do I replace the ActivePart's FrameworkElement in an instance of the control with a new FrameworkElement?
I realise the Control.Template property is writeable, but I'd prefer to find a way to replace a specific part of that template rather than create a whole new one, although a solution that edits the existing template by replacing the appropriate named part would be acceptable.
I'm using C# and need to do this at runtime using any FrameworkElement, so I'm not looking for XAML-based solutions or suggestions, thanks!
P.S. I'm aware that GetTemplateChild() returns the FrameworkElement for a named template part in a control instance, but I don't see how that helps me to replace that part with a new FrameworkElement.
I guess I was missing the obvious!
After OnApplyTemplate is called on your derived Control, VisualTreeHelper.GetChild (this, 0) will return the root object of the instantiated template's visual tree, from which you can use the Children collection (if your Control's template is Panel-derived, e.g. Canvas, Grid, etc.) to add/remove/modify any of the child template parts to your heart's content.
Before OnApplyTemplate is called, no visual tree exists for the Control, and VisualTreeHelper.GetChildrenCount returns 0.

WPF: Switch Template based on UserControl Data

Trying to implement what I thought was a simple concept. I have a user control (view) bound to a view model which provides a list of data. I have added toggle buttons to the usercontrol and would like to allow the user to use these toggle buttons to switch out which template is used to show the data. All of the templates used for the data work, and they are very different from one another so it's not just simple changes to a single template. I'd like to get this as much in XAML as possible.
Here's what I have now:
Where the data appears I have <UserControl Template="{StaticResource ListSwitchingControlTemplate}" />
In that control template I have all "sub templates" - really it's just all 3 representations with their visibility set to Collapsed. Then I use a data trigger on that control template to show the currently selected view. This works, but I noticed that all 3 representations get bound - they each act like they are active (and they are I guess).
I'd rather be able to truly switch the template at run time. I tried converting the containing user control to use a ContentTemplate for itself, but that just messes up all of the binding I have elsewhere. If only UserControls could use DataTriggers I'd be ok.
Any suggestions on how to cleanly go about getting this behavior. I have an idea that I'm just missing something simple.
Thanks,
Dave
you could do it via code?
http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector ???
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4fd42590-8375-46d0-b7bc-6c217df0f0ba/
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dbbbb5d6-ab03-49a0-9e42-686fd41e0714
One way to do this would be to use a DataTemplateSelector.
Basically, you create a class that inherits from DataTemplateSelector and override its SelectTemplate virtual function. The return value from the function is the DataTemplate you want to use and in that function you have access to the object and its properties, which you can use to decide which template to select.
There is an example on MSDN here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

Resources