Say I have a grid, I click an object and it displays in a detail screen. I don't want the user to edit some data so I set the TextBox as disabled? Will binding work? Basically what I want is the TextBox to be greyed out or disabled? How about it in WPF? Can someone explain?
Yes, binding will work with a disabled textbox. For disabling the textbox you have three options:
Set the IsReadOnly property to true. This will not affect the appearance of the textbox, but will stop the user changing the value inside it.
Set IsEnabled to false. This will gray out the textbox and stop it from receiving focus
Use a label or a textblock. This will place the text on screen without the appearence of being in an editable control at all.
As for binding, this will work the same no matter what you do. Set up the binding as normal in either the Xaml or codebehind and the value will update when the backing property changes as usual (provided you have implemented INotifyPropertyChanged, otherwise it'll only get set once)
There is a IsReadOnly property on the TextBox, just set it to true
I would use a <TextBlock/> or a <Label/> to display static data instead of a <TextBox/>.
Related
I have a "User Settings" dialog which has Text Boxes to display the settings values in Settings.Default. I have set the DataContext of the Dialog to Settings.Default and set the Text property bindings to OneWay (to avoid instant update of settings in case the dialog is cancelled)
Some of the settings are directory paths which I set using the System.Windows.Forms.FolderBrowserDialog. This simply sets the Text property of the associated TextBox (accepting the Dialog forces the update of the DC). I also have a "Defaults" button which calls
Settings.Default.Reset().
If I edit the contents of a textbox manually and then click Defaults it resets the contents as expected (so I know the binding is OK). However, if I modify the Text property using the FileBrowserDialog and then click default, the Textbox contents remains at what was selected with the FileBrowserDialog even though the Setting.Default setting has reset to the default setting.
Any ideas??
You should use SetcurrentValue to set the Text dependency property value of your textbox.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.dependencyobject.setcurrentvalue?redirectedfrom=MSDN&view=netcore-3.1#System_Windows_DependencyObject_SetCurrentValue_System_Windows_DependencyProperty_System_Object_
The SetCurrentValue method changes the effective value of the
property, but existing triggers, data bindings, and styles will
continue to work.
Setting the binding to twoway is not the only way to avoid over writing a binding.
I know that you can set a textbox as multiline in SL4 by setting the AcceptsReturn field to true. However, if you insert this into a DataField and DataForm, the textbox always shows as a single-line textbox, even if I change the VerticalAlignment to Stretch.
How can I make it multiline?
Use EditTemplte and put Textbox as your DateTemplate.
As you already said, set the Textbox AcceptReturn to true, and that's it.
Sample of DisplayTemplate and EditTemplte you can watch here : (#23:00~)
http://www.silverlight.net/learn/data-networking/data-controls/dataform-control
Please update on how it went.
My WPF combobox is populated with a different set of strings each click of a button. There are other controls on the window as well. The combobox is the 'first' (top) in the window, but the text doesn't get highlighted. When the user tabs through the controls, the text DOES get highlighted, but when it's the first on the window, it doesn't.
Maybe i need to force a highlight on the individual textbox control 'within' the combobox itself, but how would i do this? I couldnt seem to find the internal 'structure' of this control anywhere. Could anyone help here?
Jack
to get the TextBox of Combobox you can use
TextBox TxtBox = (TextBox)myCombo.Template.FindName("PART_EditableTextBox", myCombo);
I'm not sure it's the best solution, but you can use FrameworkElement.FindName to access the child control -- it's guaranteed to be present in a combobox, because it's a key constituent part of the control.
That stated, is it not better to try and call .Focus() on the control? That is likely why when you tab, the highlight is provided.
Another option is to derive from ComboBox, and expose the child text box as a property allowing you to set it's selection, or add a method directly to the combobox to set it for you.
I am trying to bind to a combobox text with the IsEditable property set to true. I have a property in my viewmodel which is bound to the text.
I want to validate on the text being typed in the text of the combobox, and restrict some values that the user is typing in. So some will be allowed, and some not, and these need to set the combobox back to its old value.
I do this in the view model and I have tried setting my text property in my view model explicity to the old value or just ignoring the change and raising that the property has been changed, but for the life of me it will not refresh the text back to the old value.
Is this because the combobox is editable, and it has the text caret and focus somewhere in the text of the combobox.
Basically, I want it to refresh back to the previous text when I restrict some typing in the combobox during in editing. Anyone have any ideas to reset the text back to its old value through the ViewModel. Thanks in advance!
Thanks for your replies. But I could never get it to work instead, I made my own UserControl which comprises a textbox overlayed over a combobox, and manipulate those two controls to meet my needs. A long way to go to solve a simple problem, but it works in the end.
Is the viewmodel property you are binding to created as a DependencyProperty? This is probably the problem you are facing Two-way binding in WPF
If you don't want to create a Dependency property then you need to implement INotifyProperty changed and manually force the update in the Property changed event.
I think this is because of a 'bug' in WPF not refreshing the UI if you change the value of a property in the setter. You can workaround it by implementing an IdentityConverter that force the UI to refresh as per this arcticle.
I have a WPF ListView that is bound to a BindingList<T>. The binding works like a charm, but I have to tab out of the cell to get the bound property to update....this is an issue because most users don't tab out of the last column before clicking the save button.
How do I force the list view to "persist" the changes to the bound DataContext without doing something hackish.
Bindings in WPF have a property called "UpdateSourceTrigger" which tells the Binding when to update the thing the UI is bound to. By default, it's set to "LostFocus" for the Text property which is what you're most likely using.
Change the trigger to "PropertyChanged" in your binding like this:
Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}"
... and now the source "Foo" property will be updated as the Text changes in the UI.
There's also an "Explicit" setting for UpdateSourceTrigger which is handy if you need to hold off writing any changes to the source until, say, the user clicks the OK button.