Say I have a text box like this:
<TextBox Text="{Binding MyBoundProperty, Mode=TwoWay"/>
Is there an easy way to find and set "MyBoundProperty" in code behind?
I am working in a custom attached property for a TextBox (catching the OnTextBoxKeyUp event) and want to set the property directly in some scenarios (when a scan happens).
If it's two-way binding, changing the Text property of the TextBox should update the source.
Related
I am trying to set focus on a text box. I tried set focus for cursor
but all that does is put the cursor there and freezes it. You can't actually type anything still. You end up still having to click the text box to type. I did exactly as they did in that question. I read some stuff about logical focus and physical focus? Not quite sure how to go about doing what I need. Do I need to create an attached property and handle it that way? I am trying to avoid code behind at all costs.
Also, after clicking a "Submit" button I would like to have it set focus back to the text box again
Here is the code I tried, the FocusManager is on a grid that wraps the textbox:
FocusManager.FocusedElement="{Binding ElementName=FocusedTextBox}"
<TextBox Text="{Binding serialNumber}"
x:Name="FocusedTextBox">
</TextBox>
As I said, all this did is put the cursor there and you can't type anything until you again click in the text box. Any suggestions?
Thanks,
Just give the textbox a name (e.g. "theTextBox') and call theTextBox.Focus().
If you're trying to do this in MVVM with data-binding (and you should be) then you can use an attached property to bind to a property in your view model.
I am trying to get the previous value of textbox binding. Binding is like:
<TextBox Text="{Binding UserName}" />
There is string property in viewModel and i am following strict MVVM approach.
Is there any available property or event or any workaround to get the previous textbox value in xaml?
Thanks
EDIT : I thought that i can write interaction trigger to the got keyboard focus event and i can bind the current value as previous value to some other property. With that way i can have the previous value right?
When using binding your property UserName uses an event to tell the text box for change. This event might be of INotifyPropertyChanged class or DependencyProperty, both of which have events you can listen to.
If you need more info post the code for context class
I'm trying to convert some XAML to C# code. Here's the xaml:
<ComboBox TextBlock.Foreground="{Binding DesiredForegroundBrush}"/>
I'd like to do the same thing in C# code, but I'm at a loss on how to access the TextBlock.
I tried the following:
ComboBoxInstance.TextBlock.SetBinding(TextBlock.ForegroundProperty, "DesiredForegroundBrush");
But the TextBlock is not accessible in C# code.
I also tried getting to the child of the combo box, but the GetChildrenCount returns 0:
var childrenCount = VisualTreeHelper.GetChildrenCount(ComboBoxInstance);
I did a few web searches but all I found was questions about how to bind combo boxes to TextBoxes.
I feel like there has to be an easy way to do this. Any help would be appreciated!
Update:
I have found this post:
How to I access an attached property in code behind?
But that only shows how to directly assign the property in the code behind, as opposed to set up binding on it.
use ComboBox.ForegroundProperty to bind the foreground Color. Why do you want the textbox ?
Every control derived from "Control" class will have Foreground property which is a dependency property and could be bound to any Brush value. To change the value of any control foreground we don't need to dig for the property.
<ComboBox Background="{Binding ForegroundColorBrush}"/>
and combobox foreground color will change according to binding.
I have a WPF TextBox that has it's text value bound in XAML. This works fine and, as expected, when the associated value is loaded to the field, it is formatted as desired.
My issue is when the user enters the text field to modify the value, I want to strip away the formatting, and display the raw underlying value.
To do this, I tried to set the BindingExpression.ParentBinding.StringFormat property, on the text boxes binding from within a GotFocus event. However, when I tried to set the StringFormat property to an empty string, I got the following exception:
Binding cannot be changed after it has been used.
So, after a control has been bound to a data model object, is there a straight-forward way that I can modify the string format of the TextBox? I'm willing to change how I format the value of the control, but the end desire is to strip the formatting of the data when it is being edited, and re-instating the string formatting once the user exits the field.
I would probably try it differently. It sounds like you are using the same TextBox for the formatted value and editing the raw value. It would be cleaner to use two different TextBoxes for View and Edit, or package it in a UserControl. Also, I would use two properties, e.g. RawText and FormattedText, and the UserControl would have DependencyProperties with bindings to both properties. The UserControl would automatically switch to the Edit TextBox. The question of "how does the automatic switching" work may be a challenge though. You probably need to use the GotFocus of the View TextBox as you mentioned, although it might not be a bad idea to have an actual Edit button that switches it for you.
I understand about switching to WPF. There is quite a bit of adjustment (aka learning) when switching to WPF. I would think of it as designing a form or control that is "fit for purpose". You don't have to create a new UserControl though. You could do something similar to StackOverflow where there is an Edit TextBox and then the View area, that would be equivalent to the Raw and Formatted values. You would control the Visibility of the Edit TextBox through a BoolToVisibilityConverter when you are in edit mode. Create a public bool IsEditing property on your ViewModel and bind that to the <EditTextBox Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}}" Text="{Binding RawText}" ...etc... /> After working with WPF for a while, you really appreciate data binding and it makes it hard to go back to plain WinForms (at least for me - not to say there aren't challenges though!).
I am thinking about the following issue.
If I have for example a textbox/slider/combobox which value is bound to something like
<TextBox Name=textBox Text="{Binding Text}"/>
and then do
textBox.Text = "something"
Is it going to "override" the binding or is binding "stronger" then explicit assignment
No, the binding will be overwritten.
No it won't update your binding. Binding gets updated only when it comes from View, if you set it from your code behind, it will override the text but will break the binding. You can try this sample -
Place a textbox on your view and bind it's text property to some
property in your viewmodel say value for this property is "Test"
Now place two button's on your view.
On first button click simply set the text of your textbox to
something say "Button1".
You will notice that textbox text will now be "Button1" but still the value of your CLR property will be "Test".
Now on second button click, try to set your Viewmodel property to say
"Button2". PropertyChanged will be fired but you won't notice any
change in your textbox text.
If you want to update the binding, you have to set the Dependency property from your code behind like this-
textBox.SetCurrentValue(TextBox.TextProperty, "Button2");
where textBox is your name of the TextBox.
Just simply put the value you will like to be in the textbox in the "Text" memeber of the bound object. Otherwise the binding will be overwritten, as devdigital mentioned above.