0

in my wpf application I am trying to change the window title bar depending on a specific static boolean variable. The idea is that no matter where I update this static flag the window title changes. If it's true then Title = "** Foo" else Title = "Foo".

Attempt number one:

I created a static boolean in my MainViewModel

public static bool RefreshTitleBar { get; set;}

And in my MainWindowView.xaml made this change:

<Window.Resources>
        <Style TargetType="Window">
            <Style.Triggers>
                Binding="{Binding Source={x:Static viewModel:MainViewModel.RefreshTitleBar}, UpdateSourceTrigger=PropertyChanged}" Value="True">
                    <Setter Property="Title" Value="--Foo"/>
                </DataTrigger>
                Binding="{Binding Source={x:Static viewModel:MainViewModel.RefreshTitleBar}, UpdateSourceTrigger=PropertyChanged}" Value="False">
                    <Setter Property="Title" Value="Foo"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
</Window.Resources>

But it doesn't update when RefreshTitleBar value changes. I have other properties working with binding so I know that's not the issue.

What am I missing here? Can someone help me understand why my title bar can't work? Is a title bar one of those things that simply can't be updated once it's set the first time? Many thanks in advance.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
user2529011
  • 705
  • 3
  • 11
  • 21
  • You defined RefreshTitleBar as a `const`. Is that a typo? A `const` cannot be changed. – Keithernet Mar 17 '20 at 18:38
  • @Keithernet oops, that was my bad. I meant to just say static. – user2529011 Mar 18 '20 at 12:39
  • @ASh I made new changes as it was suggested in the link you provided (see updated code above) but my window title is still not being updated correctly. Thoughts? – user2529011 Mar 18 '20 at 12:41
  • _"I made new changes as it was suggested in the link"_ -- no, you didn't. You need to read _all_ the answers, including [this one](https://stackoverflow.com/a/7515926). – Peter Duniho Mar 18 '20 at 16:40

1 Answers1

0

The reason why your code doesn't work is that your static property has no change notification. So your triggers will never fire.

You may want to consider having your RefreshTitleBar property as a standard INotifyPropertyChanged property, buy put it into a singleton class. This way you will only have one instance of the class for your whole application and you'll get the change notifications you need.

You can implement change notification for a static property, but it's a little more difficult.

Binding static property and implementing INotifyPropertyChanged

I hope this helps.

Keithernet
  • 2,349
  • 1
  • 10
  • 16