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

Feature/improve view model source generation #129

Merged
merged 2 commits into from
Feb 9, 2025
Merged
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This is a base libraries for building WPF application with the MVVM design patte

## Requirements

[.NET 8 - Desktop Runtime](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
[.NET 9 - Desktop Runtime](https://dotnet.microsoft.com/en-us/download/dotnet/9.0)

## NuGet Packages Provided in this Repository

Expand Down Expand Up @@ -150,6 +150,16 @@ Therefore run the `Atc.Wpf.Sample` application to explore all the controls and c

- [ValueConverters](src/Atc.Wpf.Theming/ValueConverters/@Readme.md)

### Source Generators

In MVVM, certain attributes help automate boilerplate code using source generators.

Example for ViewModel classes

![MVVM Source Generation](docs/images/mvvm-source-generated.png)

For more details, see the [MVVM](src/Atc.Wpf/Mvvm/@Readme.md) section.

## How to contribute

[Contribution Guidelines](https://atc-net.github.io/introduction/about-atc#how-to-contribute)
Expand Down
Binary file added docs/images/mvvm-source-generated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/Atc.Wpf.Sample/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
global using System.Globalization;
global using System.IO;
global using System.Runtime.CompilerServices;
global using System.Text;
global using System.Text.Json;
global using System.Windows;
global using System.Windows.Controls;
Expand Down
10 changes: 9 additions & 1 deletion sample/Atc.Wpf.Sample/SamplesWpf/Mvvm/PersonView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvm="clr-namespace:Atc.Wpf.Sample.SamplesWpf.Mvvm"
d:DesignHeight="450"
d:DesignHeight="550"
d:DesignWidth="800"
mc:Ignorable="d">

Expand Down Expand Up @@ -48,6 +48,14 @@

<atc:LabelTextInfo LabelText="FullName" Text="{Binding Path=FullName}" />

<atc:UniformSpacingPanel
HorizontalAlignment="Center"
HorizontalSpacing="50"
Orientation="Horizontal">
<Button Command="{Binding Path=ShowDataCommand}" Content="Show data" />
<Button Command="{Binding Path=SaveHandlerCommand}" Content="Save data" />
</atc:UniformSpacingPanel>

</atc:UniformSpacingPanel>

</ScrollViewer>
Expand Down
72 changes: 68 additions & 4 deletions sample/Atc.Wpf.Sample/SamplesWpf/Mvvm/PersonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ namespace Atc.Wpf.Sample.SamplesWpf.Mvvm;
public partial class PersonViewModel : ViewModelBase
{
[ObservableProperty]
[AlsoNotifyProperty(nameof(FullName))]
[NotifyPropertyChangedFor(nameof(FullName))]
[Required]
[MinLength(2)]
private string firstName = "John";

[ObservableProperty]
[AlsoNotifyProperty(nameof(FullName), nameof(Age))]
[AlsoNotifyProperty(nameof(Email))]
[AlsoNotifyProperty(nameof(TheProperty))]
[NotifyPropertyChangedFor(nameof(FullName), nameof(Age))]
[NotifyPropertyChangedFor(nameof(Email))]
[NotifyPropertyChangedFor(nameof(TheProperty))]
private string? lastName = "Doe";

[ObservableProperty]
Expand All @@ -23,4 +25,66 @@ public partial class PersonViewModel : ViewModelBase
private string? myTestProperty;

public string FullName => $"{FirstName} {LastName}";

[RelayCommand]
public void ShowData()
{
var sb = new StringBuilder();
sb.Append("FirstName: ");
sb.AppendLine(FirstName);
sb.Append("LastName: ");
sb.AppendLine(LastName);
sb.Append("Age: ");
sb.AppendLine(Age.ToString(GlobalizationConstants.EnglishCultureInfo));
sb.Append("Email: ");
sb.AppendLine(Email);
sb.Append("TheProperty: ");
sb.AppendLine(TheProperty);

var dialogBox = new InfoDialogBox(
Application.Current.MainWindow!,
new DialogBoxSettings(DialogBoxType.Ok)
{
Height = 300,
},
sb.ToString());

dialogBox.ShowDialog();
}

[RelayCommand(CanExecute = nameof(CanSaveHandler))]
public void SaveHandler()
{
var dialogBox = new InfoDialogBox(
Application.Current.MainWindow!,
new DialogBoxSettings(DialogBoxType.Ok),
"Hallo to SaveHandler method");

dialogBox.Show();
}

public bool CanSaveHandler()
{
if (string.IsNullOrWhiteSpace(FirstName))
{
return false;
}

if (string.IsNullOrWhiteSpace(LastName))
{
return false;
}

if (Age is < 18 or > 120)
{
return false;
}

if (Email is not null && !Email.IsEmailAddress())
{
return false;
}

return true;
}
}
136 changes: 0 additions & 136 deletions src/Atc.Wpf.SourceGenerators/Extensions/AttributeDataExtensions.cs

This file was deleted.

Loading
Loading