C
C#2w ago
Mustafa

I can't get the value I selected from the picker in the viewmodel

I select a value from the Picker. I see that it changes in the OnPropertyChanged() method in my ViewModel, but the value I select in the Task creation method is reset
No description
No description
No description
43 Replies
canton7
canton72w ago
Please post full source files, not little snippets. You may know how the different snippets tie together, but we have no idea
Casianm8
Casianm82w ago
Hi
Mustafa
Mustafa2w ago
These are my taskviewmodel
No description
No description
Mustafa
Mustafa2w ago
Hi
canton7
canton72w ago
Can you post as text files, rather than images of text? The images are blurry, I can only look at one at a time, and I can't search them
Mustafa
Mustafa2w ago
Ok
Casianm8
Casianm82w ago
I am attaching the following code where I have updated a list of collections (ObservableCollection), in the form of an array, to be able to store each Int32 element. Additionally, I have added a method to allow us to select each Int32 element.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Help
{
public class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}




// It's used for Collection
private ObservableCollection<Int32> _Priorities = new ObservableCollection<Int32>();
public ObservableCollection<Int32> Priorities {
get { return _Priorities; }
set { _Priorities = value;
OnPropertyChanged(nameof(Priorities));
}
}

// It's used to select Int32 object from collection


private Int32 _Selected_Priorities;
public Int32 Selected_Priorities
{
get { return _Selected_Priorities; }
set { _Selected_Priorities = value;
OnPropertyChanged(nameof(Selected_Priorities));
}

}

public Test()
{
// Constructor

Priorities.Add(1);
Priorities.Add(3);
Priorities.Add(5);

foreach (var priority in Priorities)
{
Console.WriteLine($"Prioritate: {priority}");
}


}


}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Help
{
public class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}




// It's used for Collection
private ObservableCollection<Int32> _Priorities = new ObservableCollection<Int32>();
public ObservableCollection<Int32> Priorities {
get { return _Priorities; }
set { _Priorities = value;
OnPropertyChanged(nameof(Priorities));
}
}

// It's used to select Int32 object from collection


private Int32 _Selected_Priorities;
public Int32 Selected_Priorities
{
get { return _Selected_Priorities; }
set { _Selected_Priorities = value;
OnPropertyChanged(nameof(Selected_Priorities));
}

}

public Test()
{
// Constructor

Priorities.Add(1);
Priorities.Add(3);
Priorities.Add(5);

foreach (var priority in Priorities)
{
Console.WriteLine($"Prioritate: {priority}");
}


}


}
}
Mustafa
Mustafa2w ago
public class ParentViewModel { public CustomerViewModel CustomerViewModel { get; } public ProjectViewModel ProjectViewModel { get; } public ActivityViewModel ActivityViewModel { get; } public CategoryViewModel CategoryViewModel { get; } public TaskViewModel TaskViewModel { get; } public ParentViewModel() { var httpClient = new HttpClient { BaseAddress = new Uri("https://localhost:7017/") }; CustomerViewModel = new CustomerViewModel(new CustomerRepository(httpClient)); ProjectViewModel = new ProjectViewModel(new ProjectRepository(httpClient)); ActivityViewModel = new ActivityViewModel(new ActivityRepository(httpClient)); CategoryViewModel = new CategoryViewModel(new CategoryRepository(httpClient)); TaskViewModel = new TaskViewModel(new TaskRepository(httpClient)); }
}
Casianm8
Casianm82w ago
And u can modify xaml code like this:
ItemsSource="{Binding Priorities, }"
SelectedItem="{Binding NewPriority, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
ItemsSource="{Binding Priorities, }"
SelectedItem="{Binding NewPriority, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
canton7
canton72w ago
And what's the problem, exactly? I can't see any code which reads NewPriority, but you said that it's "reset" in "the Task creation method"?
Mustafa
Mustafa2w ago
I am getting data from several viewmodels in the View, so I connected it to the parent viewmodel And when I select the priority value from the Picker, I see that the value changes in onpropertychanged, but the NewTaske priority is 0.
Casianm8
Casianm82w ago
U need to have OnPropertyChanged() on every ObservableCollection
Mustafa
Mustafa2w ago
If I connect my Priority view directly to the taskviewmodel, my problem is solved, but this time the other picker objects in my view cannot receive the objects they receive from other viewmodels.
canton7
canton72w ago
Only if it's assigned. ObservableCollections often aren't assigned
Want results from more Discord servers?
Add your server