RelayCommand CanExecute when using a contentView in Avalonia

I have an Avalonia application where I am changing the ContentView model to different ViewModels.

I start off with a View with two buttons to display either Live or Post analysis. On pressing Live Analysis, the LiveAnalysisView is displayed. On this view there is a button to switch to post analysis which should only be enabled when data capture is not active. So on first loading the page it should be enabled but on pressing the connect button is should disable.

I’ve used the Avalonia ToDoList as a starting point for changing the ContextViewModel and am using the CommunityToolkit.MVVM

I’ve created the RelayCommand and associated CanExecute but believe I need a NotifyCanExecuteChanged somewhere but can’t figure out how to hook it all up. Can someone point me in the right direction or tell me what I am doing wrong? Thanks

MainWindowViewModel

  [ObservableProperty] private ViewModelBase _contentViewModel;
   
    private LiveAnalysisViewModel LiveAnalysis { get; }
  
    private PostAnalysisViewModel PostAnalysis { get; }
  
    [ObservableProperty] private WindowState _currentWindowState;
          
    public MainWindowViewModel(INetworkInterfaceService networkInterfaceService,
      IMessageSetProcessorFactory messageSetProcessorFactory, IPacketCapture packetCapture, ITimer 
     )
    {
    
      CurrentWindowState = WindowState.Normal;
     
      LiveAnalysis = new LiveAnalysisViewModel(config, networkInterfaceService,    messageSetProcessorFactory, packetCapture);
      PostAnalysis = new PostAnalysisViewModel();
      _contentViewModel = new StartUpViewModel();
    }


    [RelayCommand(CanExecute = nameof(IsShowPostAnalysisExecutable))]
    private void ShowPostAnalysis()
    {
      ContentViewModel = PostAnalysis;
      CurrentWindowState = WindowState.Maximized;
    }

    private bool IsShowPostAnalysisExecutable()
    {
      return LiveAnalysis.HasLiveAnalysisStopped;
    }

LiveAnalysisView.axaml

I’ve left the original Command binding I used from the ToDoList tutorial commented out below the code I am now trying.

  <Button Grid.Row="7" Name="AnalysisButton"
                  Classes="CenteredButton"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Bottom"
                  Background="Green"
                  x:CompileBindings="False"
                  Command="{Binding (viewModels:MainWindowViewModel).ShowPostAnalysisCommand}">
                  <!-- Command="{Binding $parent[Window].DataContext.ShowPostAnalysis}"> -->
            Analysis
          </Button>

LiveAnalysisViewModel

[ObservableProperty]
private bool _hasLiveAnalysisStopped;

private void HandleConnectButtonClick(object? sender, ConnectButtonEventArgs e)
{
  var args = e.AsMaybe().Value;
   
  if (args.ShouldPacketsBeCaptured)
  {
    HasLiveAnalysisStopped = false;   
  }
  else
  {
    HasLiveAnalysisStopped = true;
  }
}

New contributor

A Williams is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật