I created a custom control in Expression Blend which consists of multiple TextBlocks placed in a Grid. Now I added this custom control to my phone page in Visual Studio and want to access and change the text of these TextBlocks from C# code.
How do I access these sub-controls in code?
I thought I could do something like this:
MyCustomControl.TextBlock1.Text = "New Text";
But it's not that easy. So how do I do it?
The property MyCustomControl.TextBlock1 exists but is internal, not public. You can use MyCustomControl.FindName("TextBlock1") as TextBlock to locate the resources by name instead.
Does GetTemplateChild(string name); work ? You should be able to use it, to access the elements of your control's template
Try below code both should work for your requirement:
Control subControl1 = (Control)MyCustomControl.Controls[0];
or
TextBox subControl1 = (TextBox)MyCustomControl.Controls[0];
Using any code you are able to access Text Property.
Always keep remember the hierarchy in your custom control and then try to access all control level-by-level.
If still you are facing any issue feel free to ask.
Related
I am completely noob in WPF. I have just opened MS Visual Studio Express 2013 Preview and I am trying to do some "Hello World!" app. I have noticed when I am adding a control to form, for example a TextBox, that control has no name assign.
Is this normal? And how can I change its properties from code?
In WPF, a control needs a name to be provided almost exclusively for the following reasons only:
The developer wants to reference the control in code-behind (frowned upon when using MVVM but sometimes necessary)
When the developer will be passing the XAML to a testing team that is using an automated UI testing tool
When a Binding on another control is using ElementName to reference a property on that control.
If it's not one of those, there's really no need to name your controls. You'll find, once you start using MVVM as your principal design pattern, that you rarely need to know the names of your controls in code. Start getting used to changing properties in XAML, not code.
You should use the Name attribute and provide a unique identifier:
<TextBox Name="UniqueName">
Value
</TextBox>
Assigning the Name value in code could only be achieved if you found a way to actually locate the control without a name in the first place - a little long-winded rather than using the markup, unless, that is, you're adding the controls to the window dynamically anyway, and so already have a direct reference to the element.
Add "Name" attribute
Msdn doc :
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.name.aspx
that control has no name assign. Is it normal? How can I change its properties from code
after creating the control, you can access all it's properties like
Textbox tb = new TextBox();
tb.Name = "textBox1";
tb.Text = "Hello world";
someStackPanel.Children.Add(tb);
I have a user control that contains multiple controls (CheckBox, Button, Label...).
I want to change the layout of this user control to support right to left languages but i can't find how to do it.
Currently i can change the controls alignement using the RightToLeft property. But how can i change their positions?
Thank you for your time.
Thank you guys for your answers but there is a better way to do it.
First we go to the user control properties and select the language property.
After changing it to another language a new resource file will be created for the user control.
After that, using the designer we can change the controls positions in the user control as we like.
The new values will be saved in the created resource file.
When the language has changed, the corresponding resource file will be loaded and the positions will be changed.
If the language is a right to left one, don't forget to specify the right to left property for the controls.
Hope this helps.
Use the layout controls with the RightToLeft property and follow the following links for implementation:
RightToLeft property in Form in C#
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.righttoleftlayout(vs.80).aspx:
Developing Arabic applications should be easy!
Implementing Mirror-Aware Controls for Windows Application with Visual Studio .NET
Try to use FlowLayoutPanel or TableLayoutPanel to hold your controls and change panels RightToLeft property
User control mirroring is not supported for user controls, see this connect article: http://connect.microsoft.com/VisualStudio/feedback/details/121202/usercontrol-mirroring-is-not-inherited-from-the-form
Their suggested workaround is to use a table layout panel which will mirror the controls in right to left.
I'm dynamically generating a Windows Forms form by reading an XML file. (Actually I have a separate tool developed to serialize a Windows Forms form and its components.)
How do I find out in which container control each control is in?
For example, if I'm dynamically creating a label I want to find if it is inside a group box or any container. Is there a property available for it?
Have a look at
Control.Parent Property
That would be mycontrol.Parent.
The controls of .NET are having a property called Parent.
The controls inside the group panel are set to their parent as the Group box name.
First, you can find a group box, say for example:
groupbox x
After that, find a label, like
(Label) x.findControl("labelID").
You can try it like this.
Visual Studio QuickWatch window has a hierarchical property grid control. Is a control like that available somewhere?
The default property grid control doesn't seem to work for me as it requires the objects to have an ExpandableConverter attribute to work the way I want. Although, if any of you know a way to turn the property grid into a QuickWatch-like control it would also be accepted.
Thanks.
It is very spread type of control. Each Library-of-Controls company created at least one.
http://images.google.com.ua/images?q=tree%20like%20grid
Here you have two samples:
http://www.codeproject.com/KB/grid/PropertyGridExWinForms.aspx
http://www.howtocode.net/software-development/c/propertygrid-utilities
I don't think so, its something you would have to create your self.
looks like it wouldn't be to difficult, I think its DataGridView where the first column is a custom cell that when clicked does a hit test to see if its hit a node glyph.
But i could be wrong.
i wonder if there is a way to access a control's templatepart from within c# for modifying the part (e.g. hiding, etc..). is it possible to get a reference to the part with pure c#?
i don't want to touch the controls template.
thanks
j.
It is possible, but its quite nasty.
On the Template there is a method called FindName, which needs two arguments: the name and the FrameworkElement that has the ControlTemplate as Template. Of course, you need to set the name of the element in the ControlTemplate...
Another more elegent solution is to use a Binding in the ControlTemplate to determine the visibility.. That way you do not need to do stuff in your code behind and you can keep it Xaml only...