Directly addressing controls in a UserControl after adding TableLayoutPanel - winforms

I recently refactored the code of a user control in my WinForms project, and changed it from a user control with text boxes, combo and buttons that were just placed all over it, to a user control that now contains a TableLayoutPanel, which holds all the controls in a better order.
My problem is that in many places the code address the controls nested in the user control directly via the Controls dictionary - for example: MyUserControl.Controls["NameOfTextBox"].Visible = false;
Now, after I nested the text boxes and buttons in a TableLayoutPanel, I can't do such addressing anymore, and now I should write MyUserControl.Controls[0].Controls["NameOfTextBox"].Visible = false;, because otherwise I get an exception.
My question is whether I should change all my code in every place that addresses the contents of the user control, or can you offer me some workaround to implement on the user control itself, so when I'll try address the controls directly, it will forward it to the contents of the TableLayoutPanel.
Any Ideas?

My problem is that in many places the code address the controls nested in the user control directly...
That's probably the core issue. Try creating properties for you UserControl instead:
public bool NameBoxVisible {
get { return NameOfTextBox.Visible; }
set { NameOfTextBox.Visible = value; }
}
Then you can reference the controls directly in your UserControl but provide a separation of concern to the consumers of your control.

Related

Winform passing data between user controls

I have windows form application which has several user controls - each one is displayed when the relevant option is selected from a listbox.
Some of the user controls need to have access to data stored in a different user control so User Control A needs to know a value of a textbox stored in User Control B. I have done my exposing some properties in the user control B. This all works fine when the application first loads and no values are changed.
The problem I am having is if the value of the textbox in user control B is changed it is not picked up by user control A.
Do I have to do something with NotifyPropertyChanged? Any suggestions please?
Two solutions here:
Create a series of public properties and handle passing values where the Form objects are newed up.
Create an event to communicate when things change and register an event handler in the target Form to accept the change. This is similar in theme to the INotifyPropertyChanged interface but that's only required/advised for formal databinding scenarios.
I prefer events for this kind of thing.

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

Show/hide controls based on user role in WPF

I need to do a quick sample WPF application where the controls on the forms should be made visible or hidden based on the user roles.
Something like this will be great,
How to manipulate WPF GUI based on user roles
I am not sure where to put the XAML defined in the thread(<Control ) so that the every control in the form uses RoleToVisibilityConverter to show or hide the controls.
i am very new to windows dev..could you please help me ?
Regards
Bala
You could solve this by binding the Visibility to a corresponding property in your code-behind/ViewModel.
This is an example from a binding in one of my testcontrols using WPF (in combination with Caliburn):
Visibility="{Binding Path=IsAdmin}"
Here I have a bool property in my ViewModel called IsAdmin.
For me, the easiest way was creating a global variable in my app. For that, go to your Project->Properties->Settings.settings and create a new variable (called, for example, Administrator), and set it to True of False depending on what you want to do. Something like this:
Then, at your MainWindow, when its initialized, you can write this:
if (!Properties.Settings.Default.Administrator)
{
DisableSettings();
}
And then, disable the fields you want to.

Sharing same vector control between different places

I'm trying to implement the following: I have an Items Manager, that has an Item class inside. Item class can store two possible visual representations of it - BitmapImage(bitmap) and UserControl(vector).
Then later, in the game, I need to share the same image or vector control between all possible places it takes place. For example, consider 10 trees on the map, and all point to the same vector control. Or in some cases this can be bitmap image source.
So, the problem is that BitmapImage source can be easily shared in the application by multiple UIElements. However, when I try to share vector control, it fails, and says Child Element is already a Child element of another control. I want to know how to organize this in the best way. For example replace UserControl with other type of control, or storage, however I need to be sure it supports Storyboard animations inside.
The code looks like this:
if (bi.item.BitmapSource != null)
{
Image previewImage = new Image();
previewImage.Source = bi.item.BitmapSource;
itemPane.ItemPreviewCanvas.Children.Add(previewImage);
} else
if (bi.item.VectorSource != null)
{
UserControl previewControl = bi.item.VectorSource;
itemPane.ItemPreviewCanvas.Children.Add(previewControl);
}
Or it is not possible to share same control in different places, then what is the best way to make a copy, or the best way to store vector data.
Thanks in advance
So, I found the problem. It is possible to attach the same UserControl to different controls.
However, when on update I was deleting control, and then filling up it again with a new pointer, that sometimes was the same as before deleting, somehow it was still in memory. And so it was like 2 same user control attached to the same parent.
I added a line of code that was cleaning all children in control, before updating it with new vector UserControl, and now works like a charm.

WPF tutorial for creating a custom usercontrol

I'm looking for a tutorial that explains creating custom usercontrols in WPF.
I want to have one control that combines a textblock, a textbox, and a button that launches a common fileopen dialog. I have the layout done and everything wired up. It's works but it's three independent controls. Hopefully there is a tutorial out there that explains how to turn this into one usercontrol and wire up everything so I can expose certain properties of the control, like the text in the textbox, to the rest of my WPF app.
This looks like a good article that might help.
http://www.c-sharpcorner.com/uploadfile/mahesh/user-control-in-wpf/
In fact, it looks like he's doing exactly what you're trying to do. As for accessing the TextBox contents from outside the user control, create a public property as shown in the article.
public string FileName
{
get { return FBCTextBox.Text; }
set { FBCTextBox.Text = value; }
}

Resources