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;
}
}
A Williams is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.