Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show and hide a split view pane with mvvm #99

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Avalonia.Samples/Avalonia.Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ViewInteraction", "ViewInte
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CompleteApps", "CompleteApps", "{50FCF785-BBCF-4AFE-AC72-79EA9E92C43A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleToDoList", "CompleteApps\SimpleToDoList\SimpleToDoList.csproj", "{7D75B38A-304C-44DE-AC2F-8A461C7FC889}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleToDoList", "CompleteApps\SimpleToDoList\SimpleToDoList.csproj", "{7D75B38A-304C-44DE-AC2F-8A461C7FC889}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RectPainter", "Drawing\RectPainter\RectPainter.csproj", "{2B746401-384F-484A-810E-7A65288165E0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Show and hide a SplitView Pane with MVVM", "Show and hide a SplitView Pane with MVVM\Show and hide a SplitView Pane with MVVM.csproj", "{35DC1F49-4DAA-4E7E-AF11-5EE36AE5A13D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Show and hide a SplitView Pane with MVVM without Reactive UI", "Show and hide a SplitView Pane with MVVM without Reactive UI\Show and hide a SplitView Pane with MVVM without Reactive UI.csproj", "{1437F7D6-D0C7-4C17-949B-E4B5379AEA06}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -137,6 +141,14 @@ Global
{2B746401-384F-484A-810E-7A65288165E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B746401-384F-484A-810E-7A65288165E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B746401-384F-484A-810E-7A65288165E0}.Release|Any CPU.Build.0 = Release|Any CPU
{35DC1F49-4DAA-4E7E-AF11-5EE36AE5A13D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35DC1F49-4DAA-4E7E-AF11-5EE36AE5A13D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35DC1F49-4DAA-4E7E-AF11-5EE36AE5A13D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35DC1F49-4DAA-4E7E-AF11-5EE36AE5A13D}.Release|Any CPU.Build.0 = Release|Any CPU
{1437F7D6-D0C7-4C17-949B-E4B5379AEA06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1437F7D6-D0C7-4C17-949B-E4B5379AEA06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1437F7D6-D0C7-4C17-949B-E4B5379AEA06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1437F7D6-D0C7-4C17-949B-E4B5379AEA06}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -158,8 +170,6 @@ Global
{B8CB5C57-07ED-4BC6-ACE8-F05E428E3EB5} = {85B157B3-F701-4F75-B4F1-EC2287729480}
{BDA7536E-26FD-436F-AAC8-F8A2B500548E} = {85B157B3-F701-4F75-B4F1-EC2287729480}
{F5CB3DA2-EB59-4792-A1B3-49F600F7C130} = {85B157B3-F701-4F75-B4F1-EC2287729480}
{48432457-6A55-4D03-9D40-260CE8E06440} = {2E99F15F-A82A-4734-A837-C0F768702600}
{0BC90E92-D8B3-4C6D-8C47-BAF57CD73CBA} = {2E99F15F-A82A-4734-A837-C0F768702600}
{7D75B38A-304C-44DE-AC2F-8A461C7FC889} = {50FCF785-BBCF-4AFE-AC72-79EA9E92C43A}
{2B746401-384F-484A-810E-7A65288165E0} = {D02161B3-8242-4BF5-96E9-780465A5023B}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.App"
xmlns:local="using:Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.ViewModels;
using Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.Views;

namespace Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI
{
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
BindingPlugins.DataValidators.RemoveAt(0);
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
}

base.OnFrameworkInitializationCompleted();
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Avalonia;
using Avalonia.Data;
using Avalonia.Data.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.Models
{
internal class SplitViewIconConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if ((bool)value)
return ">";

return "<";
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Avalonia;
using System;

namespace Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI
{
internal sealed class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup>

<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.1.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.1.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.0" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.1.0" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Show and hide a SplitView Pane with MVVM without Reactive UI", "Show and hide a SplitView Pane with MVVM without Reactive UI.csproj", "{CB2EF2F1-8A53-4FAB-8D64-1B2DFFFD829B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CB2EF2F1-8A53-4FAB-8D64-1B2DFFFD829B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB2EF2F1-8A53-4FAB-8D64-1B2DFFFD829B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB2EF2F1-8A53-4FAB-8D64-1B2DFFFD829B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB2EF2F1-8A53-4FAB-8D64-1B2DFFFD829B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D27C14D-E3A2-472B-A2F0-645A6A5AC99C}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.ViewModels;
using System;

namespace Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI
{
public class ViewLocator : IDataTemplate
{

public Control? Build(object? data)
{
if (data is null)
return null;

var name = data.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
var type = Type.GetType(name);

if (type != null)
{
var control = (Control)Activator.CreateInstance(type)!;
control.DataContext = data;
return control;
}

return new TextBlock { Text = "Not Found: " + name };
}

public bool Match(object? data)
{
return data is ViewModelBase;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.ComponentModel;

namespace Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.ViewModels
{
public class MainWindowViewModel : INotifyPropertyChanged
{
private bool _isSplitViewPaneOpen;
public bool IsSplitViewPaneOpen
{
get => this._isSplitViewPaneOpen;
set
{
this._isSplitViewPaneOpen = value;
OnPropertyChanged(nameof(IsSplitViewPaneOpen));
}
}

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public MainWindowViewModel()
{
// The default value of a boolean variable is false. Hence if you need the Pane to start closed you can avoid this initialization.
this._isSplitViewPaneOpen = false;

}
public void ChangeSplitViewPaneStatusCommand()
{
this.IsSplitViewPaneOpen = !this.IsSplitViewPaneOpen;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

namespace Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.ViewModels
{
public class ViewModelBase
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico"
Title="Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI"
xmlns:convs="clr-namespace:Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.Models">

<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainWindowViewModel/>
</Design.DataContext>

<Window.Resources>
<convs:SplitViewIconConverter x:Key="SplitViewIconConverter"/>
</Window.Resources>

<SplitView PanePlacement="Right" DisplayMode="CompactInline" IsPaneOpen="{Binding IsSplitViewPaneOpen}">
<SplitView.Pane>
<StackPanel VerticalAlignment="Top" Margin="5" Orientation="Horizontal">
<Button Command="{Binding ChangeSplitViewPaneStatusCommand}" Content="{Binding Converter={StaticResource SplitViewIconConverter} Path=IsSplitViewPaneOpen} "/>
<TextBlock VerticalAlignment="Center" Margin="15, 0, 0, 0" FontSize="25" Text="Settings"/>
</StackPanel>
</SplitView.Pane>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="MainWindow" FontSize="25"/>
</SplitView>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Avalonia.Controls;

namespace Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embedded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="Show_and_hide_a_SplitView_Pane_with_MVVM_without_Reactive_UI.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Show_and_hide_a_SplitView_Pane_with_MVVM.App"
xmlns:local="using:Show_and_hide_a_SplitView_Pane_with_MVVM"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Show_and_hide_a_SplitView_Pane_with_MVVM.ViewModels;
using Show_and_hide_a_SplitView_Pane_with_MVVM.Views;

namespace Show_and_hide_a_SplitView_Pane_with_MVVM
{
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
}

base.OnFrameworkInitializationCompleted();
}
}
}
Binary file not shown.
Loading