ContextMenuPage.xaml.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.ComponentModel;
  3. using Avalonia.Controls;
  4. using ControlCatalog.ViewModels;
  5. namespace ControlCatalog.Pages
  6. {
  7. public partial class ContextMenuPage : UserControl
  8. {
  9. public ContextMenuPage()
  10. {
  11. InitializeComponent();
  12. DataContext = new ContextPageViewModel();
  13. }
  14. private ContextPageViewModel? _model;
  15. protected override void OnDataContextChanged(EventArgs e)
  16. {
  17. if (_model != null)
  18. _model.View = null;
  19. _model = DataContext as ContextPageViewModel;
  20. if (_model != null)
  21. _model.View = this;
  22. base.OnDataContextChanged(e);
  23. }
  24. private void ContextFlyoutPage_Closing(object? sender, CancelEventArgs e)
  25. {
  26. e.Cancel = CancelCloseCheckBox?.IsChecked ?? false;
  27. }
  28. private void ContextFlyoutPage_Opening(object? sender, CancelEventArgs e)
  29. {
  30. e.Cancel = CancelOpenCheckBox?.IsChecked ?? false;
  31. }
  32. public void CustomContextRequested(object? sender, ContextRequestedEventArgs e)
  33. {
  34. if (sender is Border border && border.Child is TextBlock textBlock)
  35. {
  36. textBlock.Text = e.TryGetPosition(border, out var point)
  37. ? $"Context was requested with pointer at: {point.X:N0}, {point.Y:N0}"
  38. : "Context was requested without pointer";
  39. e.Handled = true;
  40. }
  41. }
  42. }
  43. }