main
syneffort 2 years ago
parent 1df85fe35b
commit b3d721afbb
  1. 9
      PacticeSolution/CommandSample/App.xaml
  2. 17
      PacticeSolution/CommandSample/App.xaml.cs
  3. 10
      PacticeSolution/CommandSample/AssemblyInfo.cs
  4. 10
      PacticeSolution/CommandSample/CommandSample.csproj
  5. 41
      PacticeSolution/CommandSample/Commands/MessageCommand.cs
  6. 20
      PacticeSolution/CommandSample/MainWindow.xaml
  7. 28
      PacticeSolution/CommandSample/MainWindow.xaml.cs
  8. 33
      PacticeSolution/CommandSample/ViewModels/MessageViewModel.cs
  9. 6
      PacticeSolution/PacticeSolution.sln

@ -0,0 +1,9 @@
<Application x:Class="CommandSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CommandSample"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace CommandSample
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace CommandSample.Commands
{
internal class MessageCommand : ICommand
{
private Action<string> _execute;
private Predicate<string> _canExecute;
public event EventHandler? CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public MessageCommand(Action<string> execute, Predicate<string> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object? parameter)
{
bool result = _canExecute.Invoke(parameter as string);
return result;
}
public void Execute(object? parameter)
{
if (_execute == null)
return;
_execute.Invoke(parameter as string);
}
}
}

@ -0,0 +1,20 @@
<Window x:Class="CommandSample.MainWindow"
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:CommandSample"
xmlns:vm="clr-namespace:CommandSample.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Window.DataContext>
<vm:MessageViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox x:Name="tbMain"/>
<Button Content="Click"
Command="{Binding DisplayMessageCommand}"
CommandParameter="{Binding ElementName=tbMain, Path=Text}"/>
</StackPanel>
</Window>

@ -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.Navigation;
using System.Windows.Shapes;
namespace CommandSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

@ -0,0 +1,33 @@
using CommandSample.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace CommandSample.ViewModels
{
internal class MessageViewModel
{
public MessageCommand DisplayMessageCommand { get; set; }
public MessageViewModel()
{
this.DisplayMessageCommand = new MessageCommand(DisplayMessage, AddMessage);
}
public void DisplayMessage(string param)
{
MessageBox.Show($"Clicked! (Param: {param})");
}
public bool AddMessage(string param)
{
if (string.IsNullOrEmpty(param))
return false;
return true;
}
}
}

@ -119,6 +119,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObervableCollectionSample",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataTemplateSample", "DataTemplateSample\DataTemplateSample.csproj", "{D6507C41-143B-45D2-9A45-F822A9E22B7E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandSample", "CommandSample\CommandSample.csproj", "{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -357,6 +359,10 @@ Global
{D6507C41-143B-45D2-9A45-F822A9E22B7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6507C41-143B-45D2-9A45-F822A9E22B7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6507C41-143B-45D2-9A45-F822A9E22B7E}.Release|Any CPU.Build.0 = Release|Any CPU
{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFB4006F-917A-4358-898F-8C4DC0A1E6C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save