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.
Related
is there any way to prevent wpf combobox from changing its text after selection changed? I have a custom control that derives from combobox and I want to be able to set the text manually after selection changed, additionally I cannot prevent the base.OnSelectionChanged from being invoked (this does the trick but it has to stay there as a part of requirements)
In general the IsEditable and the IsReadOnly properties of ComboBox are used to control the level to which the display Text of the ComboBox is editable or selectable by the user.
In the msdn combobox (section remarks) you can read about it.
I had a similar issue to solve, here's how I did it:
My First ComboBox item is an object implementing NotifiyPropertyChanged, i can change its value at any time and it updates.
I put its IsEnabled to False so that the user cannot select it.
If you want this item to be displayed the same way as others even when disabled, design your ItemTemplate.
In the SelectionChanged handler, if the selected index 0, I do nothing.
If the selectedIndex is not the first, I do my computation with this index (including updating the first item's text) then I set SelectedIndex to 0.
Edit 2 : try to set the grid's IsHitTestVisible to False, and to True for the CheckBoxes.
Edit 1 : If the first solution doesn't work : So the core issue is that when you click on a row and not on a CheckBox, it triggers SelectionChange. What you have to do is to handle the tunnelling left click event : Add a handler (in xaml more simple than in code) to PreviewMouseLeftButtonDown, and in the handler get the OriginalSource of the MouseButtonEventArgs. First Check that we are in second choice (index:1) of the CheckBox by checking if the Original source or one of its visual parent is the the second CheckBoxItem. If its not then return. Now if the OriginalSource is a CheckBox or is a visual parent a CheckBox then do nothing Otherwise mark the event as handled.
NB : You'll have to use VisualTreeHelper.GetParent and write a sub that checks if a Dependency object or one of its parent is of a given type. (the top most parent is the Window, having Nothing/Null as parent.) This sub will return the right typed object if found, or Noting/Null if not found.
I have a WPF window that has a datacontext of my class 'Item'. When a user types in a TextBox the validation triggers with no issues. I need to validate this TextBox and every other Property that is set in this bound class when the user clicks 'Save'.
I don't believe UpdateSourceTrigger is what I'm looking for, although I have it set to PropertyChanged for when the user does actually do data entry on the field.
I would rather not set all my properties to the corresponding UI control on the Save click to force the update on the property but I could.
Thanks!
Data error validation only gets executed when binding pushes new property values out to the data source. If you want to execute that validation under any other circumstances, you have to write code to do it.
If your UI properties are bound to the item's properties, then the validation already should have run for each of them when the Save button is clicked. So why do you need to run it again?
One common issue is where you've set your bindings to update on LostFocus, but the object that the user clicks on to save the item isn't focusable. In this case, the last property updated in the UI isn't updated in the data source when the item is saved, since its control hasn't lost focus and its binding hasn't fired. Is that your problem?
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.
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/>.
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.