Xamarin Forms call function on property change - mobile

I'm New to xamarin and Xamarin forms.
i wonder if i can call a function in my Content page when Entry.IsFocused
i need to make a different Control to show up or disappear according to the property change
is it possible in Xamarin Forms
Thanks
Shimon

Create a ViewModel that holds a bool value. Let's call it IsEntryFocused.
Bind the control to your ViewModel's property. For example:
entry.SetBinding(TextView.IsFocused, new Binding("IsEntryFocused", BindingMode.TwoWay));
In addition to that bind the same ViewModel's property to the IsVisible property of a different control. For example:
label.SetBinding(TextView.IsFocused, new Binding("IsEntryFocused"));
Now label appears when entry has got the focus on it.
This exampe is based on the MVVM pattern. For a short introduction in Xamarin.Forms read this: https://developer.xamarin.com/guides/cross-platform/xamarin-forms/user-interface/xaml-basics/data_bindings_to_mvvm/

Related

How to bind WPF-Datagrid Control in Editor

I am coming from programming with WinForms and now start to change into WPF. In WinForms I could easily bind a DataGrid before Runtime, to adjust the columns without coding everything.
The WPF-Datagrid has the property "ItemsSource", but I don't understand how to bind it in editor. I have already a DataSource which refers to a SQL-Database, but it will not be shown in the property window.
How to do this?
Screenshot
you have to expose your collection as a public property
make it visible in the xaml (namespace include)
set up your DataContext correctly
create the binding in XAML and set up the columns, like here
If you want to see the changes in your collection online, you have to use ObservableCollection<>

Silverlight: How to bind to a resource that is defined as a usercontrol resource in code

I have a string value that i have defined as a resource in a user controls' constructor
public void usercontrolClas()
{
this.Resources.Add("stringState", "foo");
}
I am trying to access this string as a StaticResource in the XAML of a child usercontrol of the above usercontrol.
<Button Content={.... ,ConverterParameter={StaticResource stringState}"/>
However this doesnt work. Its working if i add the resource to Application.Resources, but I have some state thats is instance specific to the usercontrol and hence has to be scoped to the usercontrol. Anyde
{StaticResource} won't work here. Once you bind it using {StaticResource} changes in resource won't be reflected. And in your case, the resource does not even exist until you create it after UserControl's InitializeComponent() method.
To make this work, you will need {DynamicResource}. But, unfortunately, it is not available in WP7 Silverlight right now.
Other way you could go is creating an Attached Property or a Custom Behavior to work around this limitation.
Tutorials on Behaviors:
Silverlight Behaviors and Triggers
Using Behaviors to enable MouseWheel in Full Screen and Out-of-browser
Silverlight 3 Drag Behavior

List<> Binding and button click using mvvm light

I am trying to use MVVM light to achieve something like this. I have the following scenario:
In my Model--I have set the properties like ActivityName,Image and there is a class constructor whose is accepting 2 parameters like name and image.
Im my DataAccess--I have set the database connection and implement the required method who will fetch the data from DB and I am storing in the List and returning the list to ViewModel.
In my ViewModel--I have created the list property who will return the list by calling the GetActivities() method that I have defined in the DataAccess.
Now my problem is I am not getting how to bind it in the View so that by clicking on a button it will display the list of activities with image. By clicking on some button a new window should open with the desired result. How to bind the above list and implement the button functionality using MVVM light.
Kindly help?
Thanks
First of all, use an ObservableCollection instead of a List as it would notify the view when property or collection changes.
Then set the DataContext of your view to the viewmodel. If you use MVVMLight View Class, then the DataContext would be automatically set. You've to just give the ViewModel Name there.
Then set the ItemsSource of the DataGrid like this <dg:DataGrid ItemsSource="{Binding YourListInViewModel}"/>
For handling click event you can use the Event-To-Command behaviour and write your logics in the Button's corresponding Command handler.
Bind to DataContext of the control

How To access commands on usercontrol from viewmodel

I have legacy windows forms user control that exposses several public methods. I wrapped this control on a wpf user control and encapsulated the Methods with a relaycommand on the new wpf usercontrol.
Now my problem is how to use the mvvm pattern to execute the commands on my user control form the viewmodel that is used with the view hosting the new wpf usercontrol.
In viewmodel you have to add a field say
Public ICommand CommandOne
Now this command will create a new RelayCommand object depending upon your requirements/conditions.
Now, you can bind this 'CommandOne' command with any object say button on your control form.
So, whenever the button is clicked then the RelayCommand object will be created and it will execute the action given to it as a parameter.
Hope it works for you.
I Fond out how to get this to work with bindings. Need to set the mode to OneWayToSource to get the command from the user control. The tricky part is that the initialization of the command has to be done inside the loaded event of the usercontrol. If you try to do it inside of the constructor, you will end up with the default initialization from the binding which could be null.
Use PRISM EventAggregator? You can fireoff an event from ViewModel, from your Usercontrol subscribe to it.
http://www.codeproject.com/Articles/355473/Prism-EventAggregator-Sample
https://msdn.microsoft.com/en-us/library/ff921122.aspx

BindableAttribute, Combobox, Selectedvalue property - WinForms

I am deriving from combobox (WinForms) and am providing a new implementation for the Selectedvalue property.
It works fine as is, but any change to the selectedvalue property is not updating other controls bound to the same "binding context" to change their values accordingly.
I did try adding the BindableAttribute(true) to the property, but still it does nottrigger the change in value to the other linked controls.
The control's DataBindings.add(...) is all set up. And other controls are also bound to the same data filed on the same datasource.
Any ideas what i am doing wrong.
Have you called your base class' implementation of overridden methods? It's possible that failing to call the base class implementation is accidentally circumventing the code that fires various event plumbing.

Resources