0

It would help if OnClick events can toggle the CheckBoxes in submenu items. Like menuitem File>>Open,New (Submenu items)

Following is the Xaml code I have written

<Grid>
    <Menu HorizontalAlignment="Left" Height="20" Margin="0,-1,-0.4,0" VerticalAlignment="Top" Width="594">
        <MenuItem Header="File" Height="20" Width="50" Click="MenuItem_Click">
            <MenuItem x:Name="ErrorBtn" Header="Error" HorizontalAlignment="Left" Height="20" IsCheckable="True" Width="139" Click="ErrorBtn_Click"/>
            <MenuItem x:Name="DebugBtn" Header="Debug" HorizontalAlignment="Left" Height="20" IsCheckable="True" Width="139" Click="DebugBtn_Click"/>
            <MenuItem x:Name="OutputBtn" Header="Output" HorizontalAlignment="Left" Height="20" IsCheckable="True" Width="139" Click="OutputBtn_Click"/>
        </MenuItem>
        <MenuItem Header="Edit" Height="20" Width="50"/>
        <MenuItem Header="Option" Height="20" Width="50">
              <MenuItem x:Name="CloseBtn" Header="Close" HorizontalAlignment="Left" Height="20" Width="139" Click="CloseBtn_Click">
        </MenuItem>
    </Menu>
</Grid>

And Observable class code is below

class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void Onpropertchanged(string PropertyName)
    {
        if(PropertyName != null)
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
}

If this click on Error, Debug, Output opens up the error window in the application. How cake I make it to close that error window if I click again on error menu item.

thatguy
  • 21,059
  • 6
  • 30
  • 40
  • What exactly is the question? The check box of the menu item you click is toggled automatically. Is the question about toggling these items, if yes, what is expected? If the question is about a mechanism how to close the window that was opened, then you should post the code that shows the window in the first place. What about the `ObservableObject` class, how does it relate to your question? – thatguy Jun 23 '21 at 10:10
  • ObservableObject class inherits from INotifyPropertyChanged, so is there any way that i can use this class to interact with the menu button click event? And yes i want to toggle the isChecked checkbox, when we click it, it should open or close the window. – Captain Sparrow Jun 23 '21 at 11:04

1 Answers1

0

Instead of using the Click routed event, you could use Checked and Unchecked instead, e.g.:

<MenuItem x:Name="ErrorBtn" Header="Error" HorizontalAlignment="Left" Height="20" IsCheckable="True" Width="139" Checked="ErrorBtn_OnChecked" Unchecked="ErrorBtn_OnUnchecked"/>

Then you can implement your logic to show and close the corresponding window in code-behind:

private void ErrorBtn_OnChecked(object sender, RoutedEventArgs e)
{
   // ...show the error window (and maybe hide the others).
}

private void ErrorBtn_OnUnchecked(object sender, RoutedEventArgs e)
{
   // ...close the window.
}

You can create a field for storing a reference to a window and use it to open or close it.

private YourErrorWindow _errorWindow;

private void ErrorBtn_OnChecked(object sender, RoutedEventArgs e)
{
   _errorWindow = new YourErrorWindow();
   _errorWindow.Show();
}

private void ErrorBtn_OnUnchecked(object sender, RoutedEventArgs e)
{
   _errorWindow.Close();
}

Depending on your requirements, you could also use the Application.Current.Windows collection to filter or search the opened windows for the one that you want to address or create your own window repository for your special windows.

If you do this in code-behind, there is no need for an observable object. In fact, if your observable object is meant to be a view model, it should not have any notion of the view. Therefore, you could in theory bind the IsChecked property of the menu items to properties on the view models and act in their setters to open or close a window depending on the new value, but this would violate the MVVM pattern if the view model instantiates or references a window directly. One common approach to this is a window service. There are plenty of examples and also lots of discussions about this topic. Here are a few, which may help to get a better understanding:

thatguy
  • 21,059
  • 6
  • 30
  • 40