I am having a problem with binding from a view to a Viewmodel property.(UWP)
<AppBarToggleButton Label="Active" Icon="People"
IsChecked="{x:Bind ViewModel.IsStatusBtnChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Click="{x:Bind ViewModel.empStautsBtnClicked}"/>
private bool isStatusBtnChecked = true;
public bool IsStatusBtnChecked
{
get { return isStatusBtnChecked; }
set { Set(ref isStatusBtnChecked, value); }
}
When I try to get the value from a method to load combobox items the value is allways the default value
private List<string> depcombo;
public List<string> Depcombo
{
get { return depcombo; }
set
{
if (depcombo != value)
{
depcombo = value;
OnPropertyChanged("Depcombo");
}
}
}
public async void GetDepCombo()
{
List<string> _dep = new List<string>();
var data2 = await SqlServerDataService.GetAllEmployeesAsync();
var depResult = (from emp in EmpItems
where emp.Status == IsStatusBtnChecked
select emp.Department).Distinct();
foreach (var item in depResult)
{
if (item != null)
{
_dep.Add(item);
}
}
Depcombo = _dep;
}
When I load the data for Employyes it works fine
public async Task LoadDataAsync(MasterDetailsViewState viewState)
{
EmpItems.Clear();
var data = await SqlServerDataService.GetAllEmployeesAsync();
data.Where(em => em.Status == IsStatusBtnChecked).ToList().ForEach(p => EmpItems.Add(p));
if (viewState == MasterDetailsViewState.Both)
{
Selected = EmpItems.FirstOrDefault();
}
}
Some help will be much appreciated
It's is confused that which is the combobox
ItemsSource, ifDepcombois ComboBoxItemsSource, You have passed a new list instance toItemsSourcewhen you callGetDepCombomethod.So, we need to set bind mode as
OneWay(OneTime is default) that could response the object instance change.If
EmpItemsis ComboBoxItemsSource, and it isObservableCollectiontype. When you callLoadDataAsync, EmpItems clear the items fist, then add the new items. And this processing does not change theEmpItemsinstance object. it could works in onetime mode.Looks like the problem is when the view is reloaded. Is there a way to refresh the view without reloading. When reloading the value of
is true but the button isChecked property is false;