Trouble with finding Automation Id for Border inside ItemTemplate - wpf

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;
}
}

Related

Accessing a 3rd-party ContentTemplate-internal control

I'm using a 3rd party control (in this case, Telerik's RadChart control), which has a complex inner ControlTemplate that encompasses several elements of the chart. Unfortunately, not all parts are exposed explicitly for me to style, and I find myself wanting to set a property on a particular sub-element inside the ControlTemplate, either via XAML or via code. After struggling with the XAML for a while, I settled for a code-centric approach, but I'm still stuck.
Using Snoop, I can see that the top-most ChartArea control (which I can access) renders an AxisX2D object named PART_AxisX, which in turn has a template which contains the PART_AxisLabels object, which is the one I need to style. (Incidentally, the property I want to set is AlternationCount, but I couldn't find a less hacky way of getting to it).
I tried to use the ControlTemplate.FindName method (as shown here) to get the AxisX2D object, and then again on that object to get the AxisLabels object, but FindName always returns null:
var chartArea = myChart.DefaultView.ChartArea;
var visualAxisX = chartArea.Template.FindName("PART_AxisX", chartArea) as AxisX2D;
even though Snoop confirms that chartArea is, in fact, the AxisX2D's TemplatedParent. I checked in the debugger and I could find PART_AxisX in chartArea's internal Template property.
So my questions are:
1) What am I doing wrong here?
2) Is there a better way to approach this that isn't as roundabout as this?
The solution is to wait till after the Loaded event has occurred on the Control.
When this event has occurred you know that the Template has been applied and the visual tree has been built for the control, and so you can then access the elements using FindName.
http://blogs.msdn.com/b/mikehillberg/archive/2006/09/19/loadedvsinitialized.aspx
Slightly related link given here for case when using a Content Template on a ContentControl.
Why would 'this.ContentTemplate.FindName' throw an InvalidOperationException on its own template?

Get Contents of a UserControl inside a StackPanel

I have a StackPanel inside a WPF page, and I am loading the same UserControl inside it depending on the user requests. Now for each of these UserControls, I need to get the items which are inside it. The UserControl consists of a Calendar, and 3 comboboxes. How can I get the items?
So far I have the following:-
foreach (UserControl child in stk.Children)
{
}
and child has the contents required, however I cannot find out how to get the contents of every child to validate and store in the database. I tried child.FindControl("combobox1") but that did not work.
Thanks for your help and time
You can get hold of the Content . However I strongly suggest you don't do those of kind of things in the UI, and you shouldn't get the data from the UI. the UI should get the data from a ViewModel or a Model, and update them when needed(This is achieved through Binding). so if you need the data, your ViewModel should be the place to look at, not the UI.
I have found the following thread which was very helpful!
How can I find WPF controls by name or type?
It uses a helper to find all the controls in a WPF page.
Thanks for all your help guys

Tool to know Windows Form Application's Form fields

I am working on a WinForm Application.
The Form has many fields/components but is poorly built.
for example a field is used as user name on one case and used as folder path on the other case. Code is quite poorly maintaned.
Is is possible that when i run the application and GUI appears, i can use a tool like 'spy++' which can show me 'names' of the components (not ids). For instance the name of a button or name of a label.
Or if i can use SPY++ for 'names' please tell me?
I would solve the problem by adding a ToolTip control to your form and iterating over each control and adding a Tool Tip message to each control that is the name of the control.
First, add a ToolTip object to your form (from the Tools section of the designer.) You can rename it, but for the sake of my demo, I left it as the default name toolTip1.
Next, add a method similar to the one I'm posting below to the code page of your form. (I'm assuming this is for C# but the code is simple and can easily be modified for VB or C++).
public void AddNameToToolTip(Control c)
{
toolTip1.SetToolTip(c, c.Name);
foreach (Control child in c.Controls) AddNameToToolTip(child);
}
Finally, from within the Form constructor, add the following line of code after the call to InitializeComponent().
AddNameToToolTip(this);
This will add a ToolTip message to each control in your form. All you should have to do is hover your mouse over each control and the ToolTip will pop up a message after a second or two displaying the name of the underlying control.
Alternatively, you can recursively adding a MouseHover event to each control and when the event is fired, write the name of the control to the debugger. This would also work if you are already using a ToolTip control within your form.

How to retrieve index of ListViewItem currently being hovered over?

I've done some digging in trying to solve this, but haven't yet found a solution that works for me.
Basically I have an <ItemsControl> and in the <ItemsControl.ItemsTemplate> I have a <ListView> which displays a number of people's names. There will always be between 0 and 5 people's names listed.
What I want to do is have a tooltip popup with additional information relevant to the entity that is being hovered over. How do I get the index (or the content like the name) of the item I am currently hovering over though, to ensure that what the tooltip is displaying is for the correct person?!
I have a MouseEnter event on the listview which is triggered every time the mouse moves over an entity & in debug mode I can explore down into the sender details & can find the person's name that I want, but how do I get to it from code?
What I want is something like:
int index = sender.GetCurrentlyHoveredOverItem();
I don't want to overcomplicate this post by listing everything I've tried, but if you want any further info, please let me know.
Thanks in advance for your help!
As others have hinted at, but not explicitly said, the items in the ListView should implement the tooltip directly, using an ItemTemplate if needed, rather than at a global level.
<ListViewItem ToolTipService.ToolTip="Tooltip for this item" />
So you have a collection (ItemsControl) of lists (ListView) and you need the tooltip to be specific to a specific element within one of the internal lists, did i understand that correctly?
If so, why not create a ListView.ItemTemplate to take care of that?
If you are using MVVM…
Bind IsMouseOver to a property in the VM, like "CurrentlyHoveredPersonList". Use OneWayToSource.
Create another VM property called "HoveredPersonListViewModel" that contains all the details you need for your tooltip. When CurrentlyHoveredPersonList gets set, populate HoveredPersonListViewModel and raise a property change notification. You'll get an actual reference to the object, so you might not need the index, but if you do you can use IndexOf(object) to get it from the source list right there in the view-model.
Bind the tooltip's DataContext to HoveredPersonListViewModel and its constituent controls to the appropriate properties thereof.

Creating Silverlight UserControl

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.

Resources