silverlight databinding not working from scriptable member - silverlight

We have exposed a silverlight page as a scriptable object. It has one scriptable member.
The page's datacontext is a viewmodel object, of typ TestViewModel with one property string Description. The TestViewModel implements INotifyPropertyChanged.
The page has a textbox bound to this Description property
When Description is set to some value from within method marked with the ScriptableMember attribute, the textbox does not change
I also have a button. When I set the Description property from the click event handler of the button, the textbox changes on my page, showing the correct value.
Any reason why databinding does not work from a scriptable member and if there is a way to make it work?

You have to make sure that "HtmlPage.RegisterScriptableObject(string scriptKey, object instance)" is set on the right place. Place it in the constructor of the xaml.cs of the page where the method with the ScriptableMember attribute is used.

Related

WPF | ChromiumWebBrowser address binding does not affect UI

I am trying to show a ChromiumWebBrowser that navigates between local HTML files.
I set a binding to the Address property in my VM, and it works only in the initial value.
when I set the address in ViewModel binded property (navigating with custom buttons) browser does not update.
what am I missing here?
(in Snoop I can see the address indeed changes, but view is same).
Your ViewModel class must implement the INotifyPropertyChanged interface and call OnPropertyChanged whenever the ViewModel property is updated.
INotifyPropertyChanged Example

Can I force update of a Control via EventTrigger to another databound Control's "LostFocus" event?

I have a Person class which does not implement INotifyPropertyChanged. My ViewModel has a property of type Person. In my View, one of the panels has MyViewModel.SelectedPerson as DataContext, so I bind some TextBoxes to Weight, Height and BodyMassIndex properties.
It so happens that BodyMassIndex is calculated from Weight and Height, but since Person doesn't notify its own changes, everytime I edit one of Height or Weight, BodyMassIndex doesn't update.
My question is: How could I request a "refresh" of textboxBodyMassIndex Text property using an EventTrigger (or any appropriate other way) which "listens" to the other two textboxes "ValueChanged" event?
Thanks for reading!
Create an extended person class (or partial to the existing for generated code as another option) which inherits from the person class and implements INotifyPropertyChanged. The new class has 'extended' properties which are based on (and change) the original properties but report the On Notify for the updating of the screen as required. The view then is bound to the extended properties which in code change the original properties; so the work is seamless.

Custom DependencyProperty not updating external view model

I have a UserControl that has a DataGrid in it filled with members. The DataGrid.ItemsSource is bound to an ObservableCollection on the model. The DataGrid.SelectedItem is bound to the SelectedMember field on the model. The SelectedMember._set calls NotifyPropertyChanged and the event calls SetValue() for the exposed DependencyProperty.
This UserControl is on a page. That page has a viewmodel too. I'm trying to bind the UserControl.CurrentMember to the viewmodel.SelectedMember but it's not changing. I can bind the CurrentMember.MemberName to a textbox and the box fills with the member name so it looks like the UserControl is exposing the DependencyProperty correctly. But if I bind to the model it doesn't update.
I can't find any cross bindings. The bind to the TextBox works fine. The field on the page model is new so there's nothing bound to it.
What could be the problem? Does the field on the page model need to be a DependencyProperty? The compiler would give me an error if that were the case.
I'll try and get a code sample but it's so ingrained I can't just post a couple of lines of code.
Tom P.
After combing the code and trying to replicate the problem in a new project I found the problem.
In the UserControl I set the DataContext to the Model. But the UserControl.DataContext gets overwritten when I put it on the page. What i needed to do was name the MainGrid and set the DataContext of the MainGrid to the UserControlModel. MainGrid, being private to the UserControl, won't get overwritten. Now it works wonderfully.

WPF MVVM UpdateSourceTrigger=Excplict

i've a contentcontrol in my Wpf-App (MVVM) which is bound to an object and displays the objects properties in textboxes, so the user can edit the values of the properties.
I want to implement undo/redo functionality with the command pattern of the GoF.
For this i need a point where i can create the command and set it into my undomanager.
My idea was to add a submitbutton. When the button is pressed, i update the sources of the textboxes (my properties) and create my command object to make the changes undoable (saving the old state of the object and the new state).
But:
- For using a submit button i need to set UpdateSourceTrigger of the textboxes to Explicit. If i want to update my sources i need to reference the controls in my view, which is bad as far as i've learned. How can i do that?
With MVVM i have to create a Command (WPF Command, not my undo redo command) for the SubmitButton but i don't see how to apply the changes to the properties from that command without referencing the textboxes (further hey are generated via datatemplates).
Thanks Walter
I assume your TextBox controls are bound to the properties in the ViewModel class. If you bind your submit button to a ViewModel Command which in turn can add appropriate command to you Command Pattern Collection and also changes some of ViewModel properties, the values in the Textbox controls will also be updated. Now, for a Textbox to update it's value when the value of a property it is bound to changes, the ViewModel class needs to implement INotifyPropertyChanged interface and raise the PropertyChanged event from the property setter with that's property's name as an argument.

Can we update a source that is not DepencyProperty or not INotifyPropertyChanged compliant

I have a business object that comes from the core of the application. That object does not inherit from INotifyPropertyChanged. It contains some property that my XAML code bind to it.
I only want to update the properties not the UI dynamically (OneWayToSource style).
By example, if I change the text of a text box, the source item does not get updated.
It is a limitation of silverlight that if the object does not implement INotifyPropertyChanged or use DepencyProperties that the source of the binding cannot be updated?
The source property does not need to be a dependency property nor does the class that exposes it need to implement INotifyPropertyChanged.
If you have the binding on the TextBox set to use the TwoWay mode editing the text box should update the bound property even if it is a plain "vanila" property. Note by default the focus has to leave the TextBox in order for the binding to update.
If your business object has a set method on the property you want to update, then the value should be updated, provided the value you enter doesn't trigger an exception.
Not implementing INotifyPropertyChanged hinders just visual feedback.

Resources