Updating databinding on LostFocus fail because of source changed before - c#

First, for information, we use MVVM light.
I have a gridview containing object databinded and one property of the selected object could be modified through a global textbox (that is outside of the gridview).
At the begining, the value of the property was updated through the textbox binding for each key pressed using the following code :
Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
But we have heavy calculs made each time the value is modified that's why we choose too update the property only on LostFocus with the code :
Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
And now I have a problem :(
When the focus goes to another control of the application other than a line of the gridview, it works. The property is well updated with the value of the textbox.
But if the focus goes to another line of the gridview, it doesn't work. In fact, when the new line is selected the content of our databinded property "Content" is updated with the value of the new selected line and it's AFTER that the databinding try to update the value when the event LostFocus occurs.
So, the LostFocus occurs AFTER that the line has changed, that the property "Content" already contains the content of the new line and that's why the update of the property "Content" for the previously selected line do nothing.
I try to do the databinding with the code :
Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
but failed to find an event fired before the "SelectionChange" where to update de property content. Do you know one?
Or do you know have an idea how to bypass this problem? A clue? Or something that could help...
Thanks!

Related

How to attach a property in WPF/XAML with NULL as initial value

I have a datagrid and I want to show some details of the currently selected row in some textboxes above the datagrid. I am working with Telerik's grid and have SelectionUnit=Mixed and SelectionMode=Extended. Thus SelectedItem and SelectedItems are always null.
My working solution is that I created an attached property which provides the values of that row if only cells from one row are selected and dummy values if cells from multiple values are selected.
that part of the grid is defined as:
<telerik:RadGridView b:myBehavior.CurrentRow="{Binding Path=Data.SelectedRow, Source={StaticResource DataContextProxy}}" Name="myGridView" ...>
and the values are referenced in the textbox with this code:
<TextBox Text="{Binding Path=(b:myBehavior.CurrentRow).TextValue, ElementName=myGridView}" />
As you can see I had to create a binding do a property in my viewModel. Is there a way to initalize the attached property without using the viewmodel? Either by providing a dummy record or NULL?
If I set b:myBehavior.CurrentRow="" I get an exception (invalid value). what do i need to do here to get it running?
Thanks to #Clemens for pointing me into the right direction. My solution to avoid an unnecessary roundtrip to the viewmodel was to create an object in the Usercontrols resources
<repositories:FahrzeugFlatRecord x:Key="myObj" />
and then bind it like so:
<telerik:RadGridView b:myBehavior.CurrentRow="{Binding Source={StaticResource myObj}}"...>
the TextBox remains unchanged. This solution was necessary as I have a PropertyChangedCallback in my behavior that registers an EventListener. If this was not the case, it would be sufficient to call to bind that behavior:
<telerik:RadGridView b:myBehavior.CurrentRow="{x:Null}"...>

WPF - TextBox not binding properly when set to readonly

I have TextBox that I'm using to add (only to add and not read) file path into DB. Text property is set when user selects certain file (OpenFileDialog). So, I set it in readonly state and it won't bind properly. When I remove readonly it works fine.
<Button Name="btnAddFile" Content="+" HorizontalAlignment="Left" Width="23" Height="23" Click="AddFilePath"/>
<TextBox Name="tbxFilePath" Height="23" Text="{Binding FilePath}" Width="364" IsReadOnly="True"/>
When I use:
Text="{Binding FilePath, Mode=OneWayToSource}"
it sometimes work but most of the time it doesn't (?!). I could use TextBlock or Label but I would really like to understand what is going on and use TextBox.
I'm using Entity Framework but don't think it does matter.
Question: How can I programmatically add text to TextBox control which is readonly and be able to bind it.
EDIT: I figured out what the problem is. When I set focus on TextBox after I set it's Text property from code-behind, it works. I guess it has to notify that Text is changed when I do it from code-behind. How to do that?
Have you tried using OneWay Binding?
MSDN reads:
OneWay Updates the binding target (target) property when the binding source (source) changes. This type of binding is appropriate if the control being bound is implicitly read-only.
Which I think covers your scenario.
The target is your TextBox Text property and your source is your FilePath property on your ViewModel.
Use:
Text="{Binding FilePath, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
EDIT
This answer assumes you have implemented INotifyPropertyChanged on your ViewModel.
EDIT
The correct binding mode is OneWayToSource. Confirmed by OP.

WPF - Editable combobox text overwritten by source on load

I have an editable combobox that also has an item source which is effectively used to prompt users with some standard entries for that box whilst giving them the freedom to enter custom entries if they wish. So, everything works fine accept when I bind to a property with a value (which is not one off the list) it sets the comboboxes text to the first item in the item source. Just to be clear, the following steps demonstrate the issue.
I open the View with a clean Model (all the properties are null/default values
I enter a custom value into the editable combobox
I save the Model
I close the View
I re-open the View with the previously saved Model
The custom value which I saved should be displayed in the editable combobox, however it is overwritten by the first value in the item source list
Note: The value in the Model property is not changed, this issue is purely visual.
So, how can I stop the itemsource overwriting custom entries?
Here is the combo box XAML:
<ComboBox IsEditable="True" ItemsSource="{Binding Contact_Types, Mode=OneTime, Source={StaticResource Resources}}" Text="{Binding Model.type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />

Two-way databinding: Updating textbox not updating bound property

I am databinding a textbox of a UserControl to a property as follows.
<TextBox Name="txtData" Text="{Binding MyData, Mode=TwoWay}"/>
But when I click the Submit button on the page that hosts the usercontrol after entering some text in the txtData textbox of the usercontrol, userControl.MyData returns null. What can I do to have the txtData.text value be assigned to property other than through TextChangedevent?
From your comments you wish to update userControl.MyData when the TextBox.Text changes.
From the binding perspective TextBox.Text is the target and MyData is the source so strictly speaking this is a OnWayToSource binding (to source => to MyData) - so there should be no need to implement INotifyPropertyChanged. But that is not the issue.
The way the binding is currently written, the MyData property expects to be found on the DataContext of the TextBox - is that the case? You could fix the data context or possibly use some other binding source e.g. ElementName or RelativeSource - difficult to say without seeing more xaml.
You might also consider setting UpdateSourceTrigger=PropertyChanged on the binding.

RibbonComboBox Text Not Retaining Edits

I have the following xaml in my ui:
<ribbon:RibbonGallery SelectedValue="{Binding Text}"
SelectedValuePath="Content"
SelectedItem="{Binding SelectedRemark, Mode=TwoWay}"
MaxColumnCount="1">
<ribbon:RibbonGalleryCategory ItemsSource="{Binding Remarks}" DisplayMemberPath="Text"/>
</ribbon:RibbonGallery>
Both SelectedRemark and Remarks are properties on my view model; and Remarks is an ObservableCollection<Remark>.
It will display properly, and I can edit the text in the combobox. However, as soon as the combobox loses focus, it reverts back to whatever the the original text was.
I'm new to WPF, and cannot figure out what am I doing wrong.
Beware of a bug that causes the selectionchanged event to fire after the mouse moves. See this bug report: https://connect.microsoft.com/VisualStudio/feedback/details/666352/
Those bindings are all related to selection. I'm not certain how the Ribbon operates but it doesn't appear that what you are trying to do will give you the results you are after.
In addition the ObservableCollection<Remark> is only relative from an Add/Remove stance when making use of binding. It will not propagate changes to the items within the collection. If you were wanting that functionality you will need to implement INotifyPropertyChanged on the Remark object, then raise a property changed notification as needed.

Categories

Resources