I have a static bool property in a Model class, which I expose to two different ViewModel classes. One of these ViewModel's has a bool property linked to said static property and is bound to the Visibility of a button via a converter. This can then be set within that ViewModel to true or false and the button's visibility changes accordingly. (The instance of this ViewModel is set in the XAML of the View, via DataContext, in which the button resides) I want to be able to change this buttons visibility from within a different View, and I thought that by having a property in my separate View's ViewModel that is also linked to my static bool in my original model, I could do this, but it isn't doing anything.
Here's my code:
MainModel
public class MainModel
{
static bool _ButtonIsVisible = true;
public static bool ButtonIsVisible
{
get { return _ButtonIsVisible; }
set { _ButtonIsVisible = value; }
}
}
MainViewModel
class MainViewModel: ObserveableObject
{
public bool ButtonIsVisible
{
get { return MainModel.ButtonIsVisible; }
set
{
MainModel.ButtonIsVisible = value;
RaisePropertyChanged("ButtonIsVisible");
}
}
}
MainView
<Window x:Class="MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:MVVM"
mc:Ignorable="d"
Title="MainWindow" Width="1920" Height="1080" WindowState="Maximized" WindowStyle="None">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
</Window.Resources>
<Button Visibility="{Binding ButtonIsVisible, Converter={StaticResource BoolToVisConverter}}" />
</Window>
ButtonIsVisible from MainViewModel is changed within a command and this works as is expected. This is where my troubles occur.
AnotherViewModel
class AnotherViewModel: ObserveableObject
{
public bool ButtonIsVisible
{
get { return MainModel.ButtonIsVisible; }
set
{
MainModel.ButtonIsVisible = value;
RaisePropertyChanged("ButtonIsVisible");
}
}
}
An instance of AnotherViewModel is created via the DataContext of it's corresponding view, and a command is bound to a button within this view in which I change the ButtonIsVisible property from AnotherViewModel, at which point I would expect my button from my MainView to change, seeing that both ViewModels get and set the values of the properties in question from a static property in my MainModel, but this isn't working. Can anyone tell me what I'm doing wrong?