I have a class containing a boolean property and I want to bind to it in app.xaml (it should provide true or false to a status indicator that is globally visible at the bottom of every page).
I got the binding to work if I make the property static (is this necessary?) but if I change the value in the ChangeStatus
-function it still keeps the initial status. How to do that right?
Here my simplified code:
My class:
namespace MyApp.ViewModels.Settings
{
class MySettingsViewModel : ObservableObject
{
public static bool MyStaticProperty { get; set; } = true;
public void ChangeStatus()
{
MyStaticProperty = false;
OnPropertyChanged(nameof(MyStaticProperty));
}
}
}
My app.xaml:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vms="clr-namespace:MyApp.ViewModels.Settings"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Page}" x:Key="PageStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Page}">
<Grid>
<Ellipse x:Name="StatusEllipse"
Height="20"
Width="20"
Stroke="Black">
<Ellipse.Style>
<Style TargetType="Ellipse">
<Style.Triggers>
<!-- Binding happens here -->
<DataTrigger Binding="{Binding Path=(vms:MySettingsViewModel.MyStaticProperty)}" Value="true">
<Setter Property="Fill" Value="GreenYellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(vms:MySettingsViewModel.MyStaticProperty)}" Value="false">
<Setter Property="Fill" Value="IndianRed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
This didn't solve my question because I don't want to bind to a property within app.xaml but to one in another class that should also change values. (I'm also following the MVVM-pattern if that makes a difference, so I don't use any code-behinds)
Edit: also this is different because my problem lays in the app.xaml I think.