Hi,@Michell . Welcome Microsoft Q&A. For the problem of click on Cancel Button(NewMenuRight.xaml) and it run the method ClickCancelar inside MainWindowViewModel.cs from current instance, I modified the code according to your situation. You could try looking at the code below.
You can check the code below to see if it is what you want. If you still have problems, please let me know.
NewMenuRight.xaml:
<UserControl x:Class="WpfApp2.Views.NewMenuRight"
...
xmlns:local="clr-namespace:WpfApp2.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid >
<CheckBox IsChecked="{Binding NovoMenuIsOpen}"/>
<Button Grid.Column="0" Margin="0,10,0,0" Width="70" Command="{Binding ClickCancelarCommand}" >Cancel</Button>
</Grid>
</UserControl>
NewMenuRight.xaml.cs:
public partial class NewMenuRight : UserControl
{
public NewMenuRight()
{
InitializeComponent();
}
}
NewChart.xaml:
<UserControl x:Class="WpfApp2.Views.NewChart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:views="clr-namespace:WpfApp2.Views"
xmlns:vm="clr-namespace:WpfApp2.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<vm:MainWindowViewModel x:Key="_instance" />
</UserControl.Resources>
<StackPanel>
<views:NewMenuRight DataContext="{StaticResource _instance}" />
</StackPanel>
</UserControl>
NewChart.xaml.cs:
public partial class NewChart : UserControl
{
public MainWindowViewModel _instance { get; set; }
public NewChart()
{
InitializeComponent();
_instance = new MainWindowViewModel();
DataContext = _instance;
}
}
RelayCommand:
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
MainWindowViewModel.cs :
namespace WpfApp2.ViewModels
{
public class MainWindowViewModel : INotifyPropertyChanged
{
private static MainWindowViewModel _instance;
private bool _novoMenuIsOpen { get; set; }
public bool NovoMenuIsOpen
{
get { return _novoMenuIsOpen; }
set
{
_novoMenuIsOpen = value;
OnPropertyChanged("NovoMenuIsOpen");
}
}
public RelayCommand ClickCancelarCommand { get; }
public MainWindowViewModel()
{
NovoMenuIsOpen = false;
_instance = this;
ClickCancelarCommand = new RelayCommand(ClickCancelar);
}
public void OpenMenu()
{
NovoMenuIsOpen = true;
}
public void ClickCancelar(object obj)
{
NovoMenuIsOpen = false;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
MainWindow.xaml:
<Window x:Class="WpfApp2.MainWindow"
...
xmlns:local="clr-namespace:WpfApp2"
xmlns:views="clr-namespace:WpfApp2.Views"
xmlns:vm="clr-namespace:WpfApp2.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<vm:MainWindowViewModel x:Key="_instance"/>
</Window.Resources>
<StackPanel>
<views:NewChart DataContext="{StaticResource _instance}" />
</StackPanel>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindowViewModel _instance { get; set; }
public MainWindow()
{
InitializeComponent();
_instance = new MainWindowViewModel();
DataContext = _instance;
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.