0

My main goal here is to add an overlay when my dialog is shown. The approach that I took is to have a bool to visibility converter that determines the visibility state of a Border which acts as an overlay.

This is the overlay for the MainWindow:

<!--MainWindow.xaml-->
<!--The MainWindow overlay-->
<Border VerticalAlignment="Stretch"
        HorizontalAlignment="Stretch"
        Background="#000000"
        Opacity="0.5"
        CornerRadius="20"
        Visibility="{Binding MainWindowOverlay,  
                             Mode=TwoWay,  
                             UpdateSourceTrigger=PropertyChanged, 
                             FallbackValue=Collapsed,
                             Converter={StaticResource BooleanToVisibilityConverter }}"/>

This is the bool to visibility converter (in App.xaml)

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

I added a static property in my MainWindowViewModel that will be used to bind for my overlay, so that I can access it whenever I want a dialog to be shown (by ShowDialog())

public static bool MainWindowOverlay { get; set; }

I know that the value MainWindowOverlay is changing because I tested it (in another ViewModel ProjectsViewModel)

//A method supplied in a delegate command 
public void ShowAddProjectsDialog()
{
    //This prints false (since false is the default value)
    MessageBox.Show(MainWindowViewModel.MainWindowOverlay.ToString());

    MainWindowViewModel.MainWindowOverlay = true;

    //Now this prints true, but the overlay in my main UI (MainWindow) does not update it
    MessageBox.Show(MainWindowViewModel.MainWindowOverlay.ToString());

    //This dialog is displayed, but the overlay is not applied to the MainWindow
    AddProjectModalDialog addProjectModalDialog = new AddProjectModalDialog();
    addProjectModalDialog.ShowDialog();
}

This is how I implement my BaseViewModel

// I am using PropertyChanged.Fody (nuget package) to add a "PropertyChanged" to all my Properties which notifies me when a change happens

[AddINotifyPropertyChangedInterface]
public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
}    




Edit (Final output, solved):

MainWindow.xaml

<!--The MainWindow overlay-->
<Border VerticalAlignment="Stretch"
        HorizontalAlignment="Stretch"
        Background="#000000"
        Opacity="0.3"
        CornerRadius="20"
        Visibility="{Binding Path=(windowViewModel:MainWindowViewModel.Overlay),  
                             FallbackValue=Collapsed,
                             Converter={StaticResource BooleanToVisibilityConverter }}" />

MainWindowViewModel.cs

//Event handler for static property (MainWindowOverlay), since PropertyChanged.Fody (nuget package) doesn't notify static property changes
//The static property name in this ViewModel is Overlay. The event name is therefore OverlayChanged (or else it will not notify the changes)

public static event EventHandler OverlayChanged;
public static bool Overlay //The data bound property in MainWindow
{
    get { return _overlay; }
    set
    {
        _overlay = value;
        if (OverlayChanged is not null)
            OverlayChanged(null, EventArgs.Empty);
    }
}

The solution I used.

paraJdox1
  • 805
  • 9
  • 23
  • MainWindowOverlay does not fire a change notification. And the Binding Path expression must be `Path=(local:MainWindowViewModel.MainWindowOverlay)` (perhaps with some other namespce prefix). `Mode=TwoWay` and `UpdateSourceTrigger=PropertyChanged` are useless. – Clemens Jun 09 '21 at 08:49
  • Do I need to change the path even if `MainWindowOverlay`'s `DataContext` is `local:MainWindowViewModel`? – paraJdox1 Jun 09 '21 at 11:17
  • btw, how do I check the version of the wpf that I am using? I found some answers (regarding this) that a static property can be binded if I have wpf 4.5 – paraJdox1 Jun 09 '21 at 11:35
  • 1
    You certainly have it already. Just try the code from the answer to the duplicate question. – Clemens Jun 09 '21 at 12:01
  • I really thought that the `PropertyChanged.Fody` (nuget package) will do the "Notifications" for me, but it turns out that I need a `static` event handler to notify me whenever a `static` property changed. Thank you! – paraJdox1 Jun 10 '21 at 05:49

0 Answers0