mvvm command (need to fix)
This commit is contained in:
9
PacticeSolution/MVVMButtonControl/App.xaml
Normal file
9
PacticeSolution/MVVMButtonControl/App.xaml
Normal file
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="MVVMButtonControl.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:MVVMButtonControl"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
17
PacticeSolution/MVVMButtonControl/App.xaml.cs
Normal file
17
PacticeSolution/MVVMButtonControl/App.xaml.cs
Normal file
@@ -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 MVVMButtonControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
10
PacticeSolution/MVVMButtonControl/AssemblyInfo.cs
Normal file
10
PacticeSolution/MVVMButtonControl/AssemblyInfo.cs
Normal file
@@ -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)
|
||||
)]
|
10
PacticeSolution/MVVMButtonControl/MVVMButtonControl.csproj
Normal file
10
PacticeSolution/MVVMButtonControl/MVVMButtonControl.csproj
Normal file
@@ -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>
|
32
PacticeSolution/MVVMButtonControl/MainWindow.xaml
Normal file
32
PacticeSolution/MVVMButtonControl/MainWindow.xaml
Normal file
@@ -0,0 +1,32 @@
|
||||
<Window x:Class="MVVMButtonControl.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:MVVMButtonControl" xmlns:viewmodel="clr-namespace:MVVMButtonControl.ViewModel"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="300" Width="200">
|
||||
|
||||
<Window.DataContext>
|
||||
<viewmodel:ModelViewMain/>
|
||||
</Window.DataContext>
|
||||
|
||||
<StackPanel>
|
||||
<Button Content="Add TextBox" Command="{Binding TestCommand}"/>
|
||||
<ItemsControl x:Name="itcMain" ItemsSource="{Binding ButtonCollection}">
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Button x:Name="btnItem"
|
||||
Content="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
FontWeight="Bold"
|
||||
FontSize="18"
|
||||
Margin="10"
|
||||
Width="100"
|
||||
Background="{Binding SelectedColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
Command="{Binding ElementName=itcMain, Path=SubCommandMethod}"
|
||||
CommandParameter="{Binding Name}"/>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
</StackPanel>
|
||||
</Window>
|
28
PacticeSolution/MVVMButtonControl/MainWindow.xaml.cs
Normal file
28
PacticeSolution/MVVMButtonControl/MainWindow.xaml.cs
Normal file
@@ -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 MVVMButtonControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
37
PacticeSolution/MVVMButtonControl/Model/Buttons.cs
Normal file
37
PacticeSolution/MVVMButtonControl/Model/Buttons.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MVVMButtonControl.Model
|
||||
{
|
||||
internal class Buttons : INotifyPropertyChanged
|
||||
{
|
||||
private Brush _selectedColor = Brushes.Transparent;
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public Brush SelectedColor
|
||||
{
|
||||
get { return _selectedColor; }
|
||||
set
|
||||
{
|
||||
if (_selectedColor != value)
|
||||
_selectedColor = value;
|
||||
|
||||
OnPropertyChanged("SelectedColor");
|
||||
}
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
|
||||
protected void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
37
PacticeSolution/MVVMButtonControl/Model/RelayCommand.cs
Normal file
37
PacticeSolution/MVVMButtonControl/Model/RelayCommand.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace MVVMButtonControl.Model
|
||||
{
|
||||
internal class RelayCommand<T> : ICommand
|
||||
{
|
||||
private Action<T> _execute;
|
||||
private Predicate<T> _canExecute;
|
||||
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
|
||||
{
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
return _canExecute?.Invoke((T)parameter) ?? true;
|
||||
}
|
||||
|
||||
public void Execute(object? parameter)
|
||||
{
|
||||
_execute((T)parameter);
|
||||
}
|
||||
}
|
||||
}
|
62
PacticeSolution/MVVMButtonControl/ViewModel/ModelViewMain.cs
Normal file
62
PacticeSolution/MVVMButtonControl/ViewModel/ModelViewMain.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using MVVMButtonControl.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MVVMButtonControl.ViewModel
|
||||
{
|
||||
internal class ModelViewMain
|
||||
{
|
||||
public ObservableCollection<Buttons> _buttonCollections;
|
||||
|
||||
public ICommand TestCommand { get; private set; }
|
||||
|
||||
public ICommand SubButtonCommand { get; private set; }
|
||||
|
||||
|
||||
public ModelViewMain()
|
||||
{
|
||||
_buttonCollections = new ObservableCollection<Buttons>();
|
||||
this.TestCommand = new RelayCommand<object>(CommandMethod);
|
||||
this.SubButtonCommand = new RelayCommand<object>(SubCommandMethod);
|
||||
}
|
||||
|
||||
public ObservableCollection<Buttons> ButtonCollection
|
||||
{
|
||||
get { return _buttonCollections; }
|
||||
set
|
||||
{
|
||||
if (_buttonCollections != null)
|
||||
_buttonCollections = value;
|
||||
}
|
||||
}
|
||||
|
||||
private void CommandMethod(object parameter)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
this.ButtonCollection.Add(new Buttons { Name = $"Button {i}", SelectedColor = Brushes.Transparent });
|
||||
}
|
||||
}
|
||||
|
||||
private void SubCommandMethod(object parameter)
|
||||
{
|
||||
for (int i = 0; i < this.ButtonCollection.Count; i++)
|
||||
{
|
||||
this.ButtonCollection[i].SelectedColor = Brushes.Transparent;
|
||||
}
|
||||
|
||||
Buttons selected = this.ButtonCollection.Where(b => b.Name.Equals(parameter.ToString())).SingleOrDefault();
|
||||
if (selected == null)
|
||||
return;
|
||||
|
||||
selected.SelectedColor = Brushes.LightCoral;
|
||||
}
|
||||
}
|
||||
}
|
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMTextInputDetection", "M
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMCanvasRectangle", "MVVMCanvasRectangle\MVVMCanvasRectangle.csproj", "{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMButtonControl", "MVVMButtonControl\MVVMButtonControl.csproj", "{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -63,6 +65,10 @@ Global
|
||||
{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Reference in New Issue
Block a user