Cannot Clear-Reset Picker in viewModel xamarin - xamarin.forms

I am trying to clear a form that has few textboxes and also a picker control.
I have this button that when the user presses it ,it should clear the form.
Whatever I do I seem to get an error
I have done the following
xaml: I have bound the picker to a selectedIndex property
ViewModel:Added a property and a method as follows
private int selectedIndex;
public int SelectedIndex
{
get { return selectedIndex; }
set { SetProperty(ref selectedIndex, value); }
}
private void ResetForm()
{
SelectedIndex = -1;
}
When I execute the code it crashes.
What is the way to reset the picker in the viewModel?
many thanks

Related

How to have Select as default option in Xamarin forms Picker Control

I have a picker control which is optional field and the user can set the selected item of picker to be empty if he wants.
Is it possible to have Select as the additional option in itemsource of xamarin forms picker control.
Code for Custom picker:
<controls:CustomPicker
Grid.Row="1"
Grid.Column="1"
Margin="0,5,0,0"
Image="Downarrow"
ItemDisplayBinding="{Binding abb}"
ItemsSource="{Binding StateList}"
Placeholder="Select"
SelectedIndex="{Binding StateSelectedIndex}"
Style="{StaticResource Key=PickerHeight}" />
StateSelectedIndex = -1;
I tried setting selected index = -1. That works only when nothing is selected in the picker control, but once if an item is selected from the picker, then option "Select" can not be chosen (disappears).
I tried referring below url Default value for Picker but this did not help.
Any help is appreciated.
Solution 1:
You should set the binding mode of SelectedIndex
SelectedIndex="{Binding StateSelectedIndex,Mode=TwoWay}"
Solution 2:
You could binding the value of SelectedItem in ViewModel .
public class YourViewModel: INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableCollection<string> MyItems { get; set; }
private string selectItem;
public string SelectItem
{
get
{
return selectItem;
}
set
{
if(value!=null)
{
selectItem = value;
NotifyPropertyChanged("SelectItem");
int SelectIndex = MyItems.IndexOf(value);
// this will been invoked when user change the select , do some thing you want
}
}
}

How to use touchup and touchDown with buttons

I have 3 buttons and i am trying to do some stuff on touchup and touchdown,i read many stuff in stackoverflow but its confusing i tried
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(button1.getX()==screenX&&button1.getY()==screenY) {
Gdx.app.log("button", "touchdown");
}
return true;
}
i also tried hit condition too and don't know right way to use it.plz help me with any suggestion
I don't understand your question. You can add a listener to your button which can catch the touchdown / touchup / checked state of a button:
Button playButton = new Button();
playButton.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
Have a look at the InputListener API, to find an explanation what these states are. For example the touchdown method:
"Called when a mouse button or a finger touch goes down on the actor. If true is returned, this listener will receive all touchDragged and touchUp events, even those not over this actor, until touchUp is received. Also when true is returned, the event is handled}."
You can also set different Button images for these states: Link
Create one InputListener and attach it to each of the three buttons. When you receive for example a touchDown, you can use the event parameter to get the listenerActor and check which button was used:
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (event.getListenerActor().equals(playButton)) {
}
}
Haven't tested it, but it should work:
public class Example {
private Button button1;
private Button button2;
private Button button3;
public Example() {
MyListener listener = new MyListener();
// create the buttons...
button1.addListener(listener);
button2.addListener(listener);
button3.addListener(listener);
}
private class MyListener extends InputListener {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (event.getListenerActor().equals(button1)) {
Gdx.app.log("Example", "button 1 touchdown");
} else if (event.getListenerActor().equals(button2)) {
Gdx.app.log("Example", "button 2 touchdown");
} else if (event.getListenerActor().equals(button3)) {
Gdx.app.log("Example", "button 3 touchdown");
}
return super.touchDown(event, x, y, pointer, button);
}
}
}
if your using input processor , you have to set the input processor to the class . this can be done by the follwing code.
//in your create method
Gdx.input.setInputProcessor(this);

How to keep button enabled after closing the form

I'm trying to make a system and I'm having trouble with buttons being disabled.
I have a function that makes the button on a another form enable the button on the main form but whenever I get back to the main form the button becomes disabled again.
How do I keep this permanent even after closing the program? Can I save it in a database to keep its function enabled even if its default is disabled?
Here's the picture of what it looks like:
Thanks for the help.
Take two buttons - "button1" and "button2" on "MainForm" Form.
Set property Enabled=false for "button1"
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 oFrm2 = new Form2();
oFrm2.evtFrm += new ShowFrm(oFrm2_evtFrm);
oFrm2.Show();
}
void oFrm2_evtFrm()
{
button1.Enabled = true;
}
}
Take one button - "button1" on "Form2" Form.
Form2.cs
public delegate void ShowFrm();
public partial class Form2 : Form
{
public event ShowFrm evtFrm;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (evtFrm != null)
{
evtFrm();
}
}
}
MainForm will display first.
Click on "button2" to display "Form2".
On "Form2", click on "button1" to make enable "button1" of "MainForm"
If you want to make "button1" enable permanent, you have to store value - "button1" is enable or disable.

WPF enable/disable button depending on value

i would like to enable/disable buttons according to the current active userlevel. i have a property in the MV for the current userlevel:
public int CurrentUserLevel
{
get { return _CurrentUserLevel; }
set
{
if (_CurrentUserLevel == value)
return;
_CurrentUserLevel = value;
RaisePropertyChanged("CurrentUserLevel");
}
}
how can i enable/disable the button if this value is >=x?
You'll need to create a property in your ViewModel, for which the Button's IsEnabled property can bind to. Make sure that the new property's PropertyChanged event is raised whenever the CurrentUserLevel is changed.
public int CurrentUserLevel
{
get { /*...*/ }
set
{
/*...*/
RaisePropertyChanged("CurrentUserLevel");
RaisePropertyChanged("IsAllowedToDoSomething"); //dependant property
}
}
public bool IsAllowedToDoSomething
{
get { return CurrentUserLevel > 1; }
}
And in your XAML:
<Button IsEnabled="{Binding IsAllowedToDoSomething}" Content="Click me!" />

.NET 4.0 DataGridCombobox SelectionChanged issue

I have a requirement in my program that the object bound (from ViewModel) in a Combobox is updated as soon as an item is selected in the combobox. Currently, the object only updates once the edit is committed by either pressing Enter or leaving the cell. The user does not want the extra step.
My thought would be to have the act of selecting an item in the combobox trigger the CommitEdit() method and then CancelEdit(). However, I cannot seem to find a way to hook into the SelectionChanged event for the DataGridComboBoxColumn as it is not available.
Other suggestions have been to listen in the viewmodel for a property change event but the property is not changed until the Cell Edit is finished.
Can anyone think of a way to cause the selection of a new item (index) in a DataGridCombobox to close the edit of the cell as if the user pressed Enter or left the cell?
NOTE: I cannot use .NET 4.5 due to customer limitations.
I've had similar issue but i just found out the solution using attached property, This may not exactly fix your problem but it will help in datagrid selection changed issue.
Below is the attached property and handler methods
public static readonly DependencyProperty ComboBoxSelectionChangedProperty = DependencyProperty.RegisterAttached("ComboBoxSelectionChangedCommand",
typeof(ICommand),
typeof(SpDataGrid),
new PropertyMetadata(new PropertyChangedCallback(AttachOrRemoveDataGridEvent)));
public static void AttachOrRemoveDataGridEvent(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
DataGrid dataGrid = obj as DataGrid;
if (dataGrid != null)
{
if (args.Property == ComboBoxSelectionChangedProperty)
{
dataGrid.SelectionChanged += OnComboBoxSelectionChanged;
}
}
else if (args.OldValue != null && args.NewValue == null)
{ if (args.Property == ComboBoxSelectionChangedProperty)
{
dataGrid.SelectionChanged -= OnComboBoxSelectionChanged;
}
}
}
private static void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
{
DependencyObject obj = sender as DependencyObject;
ICommand cmd = (ICommand)obj.GetValue(ComboBoxSelectionChangedProperty);
DataGrid grid = sender as DataGrid;
if (args.OriginalSource is ComboBox)
{
if (grid.CurrentCell.Item != DependencyProperty.UnsetValue)
{
//grid.CommitEdit(DataGridEditingUnit.Row, true);
ExecuteCommand(cmd, grid.CurrentCell.Item);
}
}
}
SpDataGrid is the custom control that i inherited from data grid.
I added below style in generic.xaml as i use the resourcedictionary for style (you can certainly add inside the datagrid).
<Style TargetType="{x:Type Custom:SpDataGrid}">
<Setter Property="Custom:SpDataGrid.ComboBoxSelectionChangedCommand" Value="{Binding ComboBoxSelectionChanged}"/>
</Style>
ComboBoxSelectionChanged is the command in my viewmodel. OnComboBoxSelectionChanged i commented the commitedit because in my case the values were already updated.
Let me know if anything is not clear or any questions. Hope this helps.

Resources