I wonder, if there is any way to bind a string to a Label or any other control.
I thought about this kind of pseudo code:
//label class value
label1.Text => LocalizedStrings.someString;
This should also edit the text shown at label1 when someString's value changes. Is there any way to do this either by something similar to the above pseudo code, using the Designer or editing the Designer.cs file?
PS: I'm using Visual Studio 15 RC, C# 6.0 and .NET 4.6.
I think there is no simple way to do that in WinForms (but if there is, I would like to know).
But you can do the contrary.
For example, you can adapt your class "LocalizedString" or extend it to get a reference to the Label. Than you add code to it so that when its content changes, it changes the content in the label.
Hum...
But also, you maybe can use this: https://msdn.microsoft.com/en-us/library/system.windows.forms.binding%28v=vs.110%29.aspx
It enables the binding of a property on a control and a property on an object. It seems exactly what you want...
Here there is an example: Bind a label to a "variable"
Try this:
Binding binding = new Binding( "Text", Properties.Settings.Default, "TranSvcAddr" );
txtTranSvcAddr.DataBindings.Add( binding );
The TextBox is bound to a Property and the value will change whenever the underlying value is changed. Works the same for a Label.
Related
In winforms c#, there is a control named ListBox. It has a method RefreshItems()
What is the real use of this method ? can someone explain with example and sample code ?
According to the documentation it does the following:
Refreshes all ListBox items and retrieves new strings for them.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.refreshitems?view=windowsdesktop-6.0
Which is a bit vague.
If we look at the RefreshItem(Int32) method, it has a remark which clears things up a bit
If the DisplayMember property is set and the property in the data source that is assigned to DisplayMember changes, use the RefreshItem method to update the value in the ListBox control.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.refreshitems?view=windowsdesktop-6.0
This suggests that if you've set a display to an item, and update the display string, you want to call the RefreshItem so that the UI will show the correct value.
RefreshItems is the same, but for all items.
I have an ItemsControl.I want to hide the Text Box in the ItemsControl DataTemplete on some Flag. But when i access the Text Box in Code Behind File .it gives the error doesn't exists in the current context.Is there a way to do this using SilverLight?
Let's go by parts, in case you are not using MVVM, due to you are using a DataTemplate the Control is not added as usual in the visual tree, so you can do the following, add to the TextBox the LoadedEvent, and create a List in order to have all the TextBoxes inside, and there you can do what you need when you want.
In case you are using Binding with MVVM, you can bind the property of that control, like the Text with Mode=TwoWay or the Visibility with a bool and adding a converter.
That is a general answer to your general question, in case you need more details, please add the specific code you are using.
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'm checking this page and I like the only answer there is.
Editable WPF treeview item on doubleclick? (with styles?)
But I don't like the idea to use c# lines, Might there be a possible to write that code using XAML?
You can use EventSetter, or AttachedBehavior. But they are assumes that you need c# code-behind.
How to do it in XAML - hard to say. One more possible way - to use x:Code clause to write c# code inside xaml.
You can use CheckBox in the XAML and using its IsChecked property change the Style to the Editable mode
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.