Ver código fonte

Refactor a bit and add a new window for DataGrid

Jonatan Gezelius 1 ano atrás
pai
commit
7f8c2e9cfd

+ 1 - 1
Awesomness.GUI/App.xaml

@@ -2,7 +2,7 @@
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:local="clr-namespace:Awesomeness.GUI"
-             StartupUri="Views/MainView.xaml">
+             StartupUri="Views/DataBoxView.xaml">
     <Application.Resources>
          
     </Application.Resources>

+ 1 - 1
Awesomness.GUI/Awsomeness.GUI.csproj → Awesomness.GUI/Awesomeness.GUI.csproj

@@ -3,7 +3,7 @@
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
     <TargetFramework>net8.0-windows</TargetFramework>
-    <RootNamespace>WpfApp2_gui</RootNamespace>
+    <RootNamespace>Awesomeness.GUI</RootNamespace>
     <Nullable>enable</Nullable>
     <ImplicitUsings>enable</ImplicitUsings>
     <UseWPF>true</UseWPF>

+ 49 - 0
Awesomness.GUI/ViewModels/DataBoxViewModel.cs

@@ -0,0 +1,49 @@
+using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Awesomeness.GUI.ViewModels;
+public partial class DataBoxViewModel : ObservableObject
+{
+    [ObservableProperty]
+    public string aFantasticString = "Awesome!";
+
+    public ObservableCollection<User> Users { get; } = new();
+
+    public DataBoxViewModel()
+    {
+        // Generate 10 random users
+        var random = new Random();
+        for (int i = 0; i < 10; i++)
+        {
+            Users.Add(new User
+            {
+                Name = $"User {i}",
+                Age = random.Next(18, 100)
+            });
+
+        }
+
+    }
+
+    [RelayCommand]
+    public void AddUser()
+    {
+        Users.Add(new User
+        {
+            Name = "New User",
+            Age = 42
+        });
+    }
+
+    [RelayCommand]
+    public void RemoveUser(User user)
+    {
+        Users.Remove(user);
+    }
+}

+ 2 - 2
Awesomness.GUI/ViewModels/MainViewModel.cs

@@ -13,9 +13,9 @@ namespace Awesomeness.GUI.ViewModels;
 partial class MainViewModel : ObservableObject
 {
     private Person scratchPerson;
-    private Person selectedPerson;
+    private Person? selectedPerson;
     private CatalogOfPeople catalog;
-    public ObservableCollection<string> AvailableNames;
+    public ObservableCollection<string> AvailableNames { get; set; }
     public MainViewModel()
     {
         scratchPerson = new Person();

+ 15 - 0
Awesomness.GUI/ViewModels/User.cs

@@ -0,0 +1,15 @@
+using CommunityToolkit.Mvvm.ComponentModel;
+using System.Runtime.Serialization;
+
+namespace Awesomeness.GUI.ViewModels;
+
+public partial class User : ObservableObject
+{
+    public Guid Id { get; } = Guid.NewGuid();
+    [ObservableProperty]
+    private string? _name;
+    [ObservableProperty]
+    private int? _age;
+    [ObservableProperty]
+    private bool isAdmin;
+}

+ 54 - 0
Awesomness.GUI/Views/DataBoxView.xaml

@@ -0,0 +1,54 @@
+<Window x:Class="Awesomeness.GUI.Views.DataBoxView"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:Awesomeness.GUI"
+        xmlns:viewmodels="clr-namespace:Awesomeness.GUI.ViewModels"
+        d:DataContext="{d:DesignInstance Type=viewmodels:DataBoxViewModel}"
+        mc:Ignorable="d"
+        Title="DataBoxView" Height="450" Width="800">
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="auto" />
+            <RowDefinition Height="20" />
+            <RowDefinition Height="*" />
+        </Grid.RowDefinitions>
+        
+        <Menu Grid.Row="0">
+            <MenuItem Header="_File">
+                <MenuItem Header="E_xit" Command="{x:Static ApplicationCommands.Close}" />
+                <MenuItem Header="New User" Command="{Binding AddUserCommand}" />
+            </MenuItem>
+        </Menu>
+        <TextBlock Grid.Row="1" Text="{Binding AFantasticString}" />
+
+        <DataGrid Grid.Row="2" ItemsSource="{Binding Users}"
+                  AutoGenerateColumns="False" 
+                  SelectionMode="Extended"
+                  SelectionUnit="CellOrRowHeader"
+                  AlternationCount="2"
+                  AlternatingRowBackground="Beige"
+                  RowBackground="Aquamarine"
+                  CanUserAddRows="False"
+                  CanUserReorderColumns="False">
+            <DataGrid.Columns>
+                <DataGridTextColumn Binding="{Binding Id, Mode=OneWay}" Header="Id" Width="auto" IsReadOnly="True" />
+                <DataGridTextColumn Binding="{Binding Name}" Header="Min första kolumn" Width="auto" />
+                <DataGridTextColumn Binding="{Binding Age}" Header="Min andra kolumn" Width="auto" />
+                <DataGridCheckBoxColumn Binding="{Binding IsAdmin}" Header="Admin" Width="auto" />
+                <DataGridTemplateColumn Header="Actions" Width="*">
+                    <DataGridTemplateColumn.CellTemplate>
+                        <DataTemplate>
+                            <StackPanel Orientation="Horizontal" Background="Red">
+                                <Button Content="Delete" Command="{Binding DataContext.RemoveUserCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
+                                        CommandParameter="{Binding}"
+                                        Margin="10,0" />
+                            </StackPanel>
+                        </DataTemplate>
+                    </DataGridTemplateColumn.CellTemplate>
+                </DataGridTemplateColumn>
+            </DataGrid.Columns>
+        </DataGrid>
+    </Grid>
+</Window>

+ 28 - 0
Awesomness.GUI/Views/DataBoxView.xaml.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+using Awesomeness.GUI.ViewModels;
+
+namespace Awesomeness.GUI.Views;
+
+/// <summary>
+/// Interaction logic for DataBoxView.xaml
+/// </summary>
+public partial class DataBoxView : Window
+{
+    public DataBoxView()
+    {
+        InitializeComponent();
+        this.DataContext = new DataBoxViewModel();
+    }
+}

+ 1 - 1
JonatansWPFochMVVMtester.sln

@@ -5,7 +5,7 @@ VisualStudioVersion = 17.9.34728.123
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Awesomeness.Core", "Awesomness.Core\Awesomeness.Core.csproj", "{5FFCB7B2-FC31-42DD-BC82-AE8D363D55DF}"
 EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Awsomeness.GUI", "Awesomness.GUI\Awsomeness.GUI.csproj", "{558A90A9-1980-4C00-8CD7-3B6E18614CCD}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Awesomeness.GUI", "Awesomness.GUI\Awesomeness.GUI.csproj", "{558A90A9-1980-4C00-8CD7-3B6E18614CCD}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution