2023. 6. 3. 22:11

1.

<Window x:Class="PlainWpfApp.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:PlainWpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <!--<StackPanel>
            <TextBlock Text="Test1" />
        </StackPanel>-->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.05*"/>
                <ColumnDefinition Width="0.9*"/>
                <ColumnDefinition Width="0.05*"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="1" Margin="0, 20, 0, 0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.2*"/>
                        <ColumnDefinition Width="0.65*"/>
                        <ColumnDefinition Width="0.15*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" VerticalAlignment="Center" Text="単一値"/>
                    <TextBox Grid.Column="1" Margin="0, 0, 15, 0" VerticalAlignment="Center"
                             Text="{Binding SingleValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Button Grid.Column="2" Content="Command" Command="{Binding ButtonCommand}" />
                </Grid>
                
            </Grid>
        </Grid>
    </Grid>
</Window>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PlainWpfApp
{
    public class ActionCommand : ICommand
    {
        private readonly Action<object> action;
        private readonly Predicate<object> canExecute;

        public ActionCommand(Action<object> action) : this(action, null) { }

        public ActionCommand(Action<object> action, Predicate<object> canExecute)
        {
            this.action = action;
            this.canExecute = canExecute;
        }


        public event EventHandler? CanExecuteChanged;

        public bool CanExecute(object? parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }

        public void Execute(object? parameter)
        {
            action(parameter);
        }
    }
}

 

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace PlainWpfApp
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {

        public ICommand ButtonCommand
        {
            get
            {
                return new ActionCommand(action => Save(), canExecute => CanSave());
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        public void Save()
        {
            MessageBox.Show("오승환 500 세이브!");
        }

        public bool CanSave()
        {
            return true;
        }

    }
}

 

 

2. CanExecute가 작동하도록 이벤트 코드 수정

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace PlainWpfApp
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {

        private string singleVal;

        public string SingleValue
        {
            get { return singleVal; }
            set
            {
                if (singleVal != value)
                {
                    singleVal = value;
                    this.NotifyPropertyChanged();
                }
            }
        }

        public ICommand ButtonCommand
        {
            get
            {
                return new ActionCommand(action => Save(), canExecute => CanSave());
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public void Save()
        {
            MessageBox.Show("오승환 500 세이브! -- 메시지=" + this.SingleValue);
        }

        public bool CanSave()
        {
            return !string.IsNullOrEmpty(this.SingleValue);
        }

    }
}

 

ActionCommand도 수정

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PlainWpfApp
{
    public class ActionCommand : ICommand
    {
        private readonly Action<object> action;
        private readonly Predicate<object> canExecute;

        public ActionCommand(Action<object> action) : this(action, null) { }

        public ActionCommand(Action<object> action, Predicate<object> canExecute)
        {
            this.action = action;
            this.canExecute = canExecute;
        }


        public event EventHandler? CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object? parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }

        public void Execute(object? parameter)
        {
            action(parameter);
        }
    }
}

'C#' 카테고리의 다른 글

Wpf DataTemplate  (0) 2023.06.04
링크 정리하기  (0) 2018.08.15
DB접속해서 테이블 데이터 참조하기  (0) 2016.12.29
C# ArrayList  (0) 2016.12.26
Posted by 다만사