SmiLe SmiLe - 1 year ago 74
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
if (instance == null)
instance = new EditModeSwitcher();

return instance;

public bool IsOriginalImgVisible
get { return this.isOriginalImgVisible; }
this.isOriginalImgVisible = value;

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

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

And where I need on my
I added

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

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

Use button to Execute the command


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


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

Answer Source

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}}"/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download