Skip to content

Commit 1590c46

Browse files
committed
Handle onchange and CanExecute
1 parent b93ceeb commit 1590c46

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A lightweight WPF ViewModel framework with automatic source generation that elim
1616
### Installation
1717

1818
```xml
19-
<PackageReference Include="SimpleViewModel" Version="0.9.5" />
19+
<PackageReference Include="SimpleViewModel" Version="0.9.6" />
2020
```
2121

2222
### Basic Usage

SimpleViewModel/BindAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
namespace SimpleViewModel;
22

33
[AttributeUsage(AttributeTargets.Field)]
4-
public sealed class BindAttribute : Attribute;
4+
public sealed class BindAttribute : Attribute
5+
{
6+
public string? OnChangeMethodName { get; init; }
7+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
namespace SimpleViewModel;
22

33
[AttributeUsage(AttributeTargets.Method)]
4-
public sealed class CommandAttribute : Attribute;
4+
public sealed class CommandAttribute : Attribute
5+
{
6+
public string? CanExecuteMethodName { get; init; }
7+
}

SimpleViewModel/SimpleViewModel.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1111
<PackageId>SimpleViewModel</PackageId>
12-
<Version>0.9.5.4</Version>
12+
<Version>0.9.6</Version>
1313
<Authors>Derek Gooding</Authors>
1414
<Company>Derek Gooding</Company>
1515
<Description>

ViewModelGenerator/ViewModelGenerator.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ public partial class {className} : BaseViewModel
7070
var fieldType = field.Type.ToDisplayString();
7171
var fieldName = ToPascal(field.Name);
7272

73-
viewModelBuilder.AppendLine($" public {fieldType} {fieldName} {{ get => {field.Name}; set => SetProperty(ref {field.Name}, value); }}");
73+
viewModelBuilder.AppendLine(
74+
$@" public {fieldType} {fieldName} {{ get => {field.Name}; set
75+
{{
76+
SetProperty(ref {field.Name}, value);");
77+
78+
var onChange = GetOnChangeMethodName(field);
79+
if (!string.IsNullOrWhiteSpace(onChange))
80+
viewModelBuilder.AppendLine($" {onChange}();");
81+
viewModelBuilder.AppendLine(" }");
7482
}
7583

7684
foreach (var method in commandMethods)
@@ -101,10 +109,12 @@ public override void Execute(object? parameter)
101109
{{
102110
vm.{method.Name}();
103111
}}
104-
}}
105-
}}
106112
");
107-
113+
var canExecute = GetCanExecuteMethodName(method);
114+
if(!string.IsNullOrWhiteSpace(canExecute))
115+
viewModelBuilder.AppendLine($" public override bool CanExecute(object? parameter) => vm.{canExecute}();");
116+
viewModelBuilder.AppendLine(" }");
117+
viewModelBuilder.AppendLine("}");
108118
spc.AddSource($"{commandClassName}.g.cs", SourceText.From(commandBuilder.ToString(), Encoding.UTF8));
109119
}
110120

@@ -122,4 +132,18 @@ private static string ToPascal(string name)
122132
if (name.StartsWith("_")) name = name.TrimStart('_');
123133
return char.ToUpperInvariant(name[0]) + name.Substring(1);
124134
}
135+
136+
private static string? GetCanExecuteMethodName(IMethodSymbol method)
137+
{
138+
var attribute = method.GetAttributes().First(x => x.AttributeClass?.Name == "CommandAttribute");
139+
var argument = attribute.NamedArguments.First(arg => arg.Key == "CanExecuteMethodName");
140+
return argument.Value.Value?.ToString();
141+
}
142+
143+
private static string? GetOnChangeMethodName(IFieldSymbol field)
144+
{
145+
var attribute = field.GetAttributes().First(x => x.AttributeClass?.Name == "BindAttribute");
146+
var argument = attribute.NamedArguments.First(arg => arg.Key == "OnChangeMethodName");
147+
return argument.Value.Value?.ToString();
148+
}
125149
}

0 commit comments

Comments
 (0)