0

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.

Berger
  • 299
  • 3
  • 13
  • No, I think it's different because it's in the *app.xaml* when I add an entry in `Application.Resources` it wants to also have an Key for the `ResourceDictionary`, if I give him any Key my application starts with just a black screen. – Berger Nov 29 '19 at 08:09
  • Since `MySettingsViewModel` is not a static class, you need to create an instance in xaml to access the `MyStaticProperty`. Another option is to use settings for that – Pavel Anikhouski Nov 29 '19 at 08:43
  • I now created an instance in *App.xaml.cs* and implemented the `NotifyStaticPropertyChanged` as stated in the [posts](https://stackoverflow.com/questions/41823609/notify-binding-for-static-properties-in-static-classes) by @Clemens but my Indicator in the View still doesn't change although the value of the `MyStaticProperty` changes in the background. – Berger Dec 02 '19 at 02:06
  • I just found out that it is updating now! But I have to switch pages to see the new values, how can I now update the page? a usual NotifyStaticPropertyChanged("MyStaticProperty") doesn't work, also not a NotifyStaticPropertyChanged(null) which should update all the values on the page. I guess it's because the binding happens in the app.xaml? – Berger Dec 02 '19 at 06:17
  • Finally I got it to work now, the problem was that I needed to define the `NotifyStaticPropertyChanged` in the same class that is using it. Before I defined it in the parent-class but it seems like it's not inheritable, a good working example can be found [here](https://github.com/timunie/BindingToStaticProperty_Example) thanks to Tim:) – Berger Dec 12 '19 at 01:06

0 Answers0