Following is the MainWindowViewModel.cs code :
using System;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using Neuroflow.Views;
using Neuroflow.Views.VisualizationViews;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.Input;
using Neuroflow.Services;
namespace Neuroflow.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
[ObservableProperty] private TabItem? _currentOpenedView;
[ObservableProperty] private ObservableCollection<TabItem> _openedViews;
public MainWindowViewModel()
{
OpenedViews = new ObservableCollection<TabItem>
{
new TabItem("Visualization", new Visualization()),
new TabItem("Linear Regression", new LinearRegression()),
new TabItem("Logistic Regression", new LogisticRegression())
};
}
}
public partial class TabItem : ViewModelBase
{
[ObservableProperty] private string _header;
[ObservableProperty] private UserControl _content;
public TabItem(string title, UserControl view)
{
Header = title;
Content = view ?? throw new ArgumentNullException(nameof(view));
}
}
MainWindow.axaml :
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:Neuroflow.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:Neuroflow.Views;assembly=Neuroflow"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
xmlns:local="using:Neuroflow.Views"
x:Class="Neuroflow.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico"
Title="Neuroflow"
WindowState="Maximized"
RequestedThemeVariant="Light">
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>
<TabControl Name="InnerContent" ItemsSource="{Binding OpenedViews}" >
<TabControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Header}" FontSize="12" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding CurrentOpenedView.Content}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</DockPanel>
</Window>
The code is properly rendering the Title, however doesn’t render the Content properly ( works fine when i hard code everything ), so the problem seems to be arising from the template code.I also tried the following ( doesn’t work either )
<TabControl x:Name="TabControl" ItemsSource="{Binding OpenedViews}"></TabControl>
Also tried the following :
<ContentControl Content="{Binding CurrentOpenedView.Content}" />
Replacing CurrentOpenedView.Content with Content doesn’t work ( Intellisence gives red lines before hand )
The error i get is :
0>MainWindow.axaml(68,37): Error AVLN2000 Avalonia: Unable to resolve property or method of name 'CurrentOpenedView' on type 'XamlX.TypeSystem.XamlPseudoType'. Line 68, position 37.
0>------- Finished building project: Neuroflow. Succeeded: False. Errors: 1. Warnings: 4