SmiLe SmiLe - 2 months ago 17
C# Question

Trouble with change properties

I have some properties in my singletone class and I want change them from another two classes.

Singletone class:

public class EditModeSwitcher:ViewModelBase//MVVMLight Libs
{
private bool isOriginalImgVisible;

private static EditModeSwitcher instance;

public EditModeSwitcher()
{
this.isOriginalImgVisible = false;
}

public static EditModeSwitcher Instance
{
get
{
if (instance == null)
instance = new EditModeSwitcher();

return instance;
}
}

public bool IsOriginalImgVisible
{
get { return this.isOriginalImgVisible; }
set
{
this.isOriginalImgVisible = value;
base.RaisePropertyChanged("IsOriginalImgVisible");
}
}
}


I left just one property because there are similar. Next I made an instance in
App.xaml


<viewModels:EditModeSwitcher x:Key="EditModeSwitcher"/>


And where I need on my
MainPage.xaml
I added

Visibility="{Binding IsOriginalImgVisible,Source={StaticResource EditModeSwitcher}, Converter={StaticResource BooleanToVisibilityConverter}}"/>


When it launch
Visibility
properties work, but I cant change it.
What Im do:

Use button to Execute the command

MainViewModel:BaseViewModel


private async void PickExecute()
{
EditModeSwitcher.Instance.IsFilterImgVisible = true;
EditModeSwitcher.Instance.IsOriginalImgVisible = true;
EditModeSwitcher.Instance.IsUserControlVisibile = false;
}


But my View doest catch this and all elements are hide(defaults value in EditModeSwitcher`s constructor), but on debugging properties changed their values

And another view model

ImageManipulatorViewModel:BaseViewModel


public async void MakeCollageExecute()
{
EditModeSwitcher.Instance.IsFilterImgVisible = false;
EditModeSwitcher.Instance.IsOriginalImgVisible = false;
EditModeSwitcher.Instance.IsUserControlVisibile = true;
}


What Im doing wrong? Why my View page doest change
Visibility
property?

Answer

You are creating two classes of your EditModeSwitcher - one within resources with line <viewModels:EditModeSwitcher x:Key="EditModeSwitcher"/> and the second in instance = new EditModeSwitcher();.

Though they have some static fields/properties, but the RisePropertyChanged needs to be run on the same one class - it's not static. It should work, if you change your binding to:

Visibility="{Binding Instance.IsOriginalImgVisible,Source={StaticResource EditModeSwitcher}, Converter={StaticResource BooleanToVisibilityConverter}}"/>