main
syneffort 2 years ago
parent 5a55141ab4
commit 777cc3c474
  1. 2
      PacticeSolution/DataGridEventTriggerSample/MainWindow.xaml
  2. 14
      PacticeSolution/DataGridEventTriggerSample/ViewModel/StudentViewModel.cs
  3. 9
      PacticeSolution/ImageLoader/App.xaml
  4. 17
      PacticeSolution/ImageLoader/App.xaml.cs
  5. 10
      PacticeSolution/ImageLoader/AssemblyInfo.cs
  6. 10
      PacticeSolution/ImageLoader/ImageLoader.csproj
  7. 34
      PacticeSolution/ImageLoader/MainWindow.xaml
  8. 104
      PacticeSolution/ImageLoader/MainWindow.xaml.cs
  9. 9
      PacticeSolution/MVVMDataGridBinding/App.xaml
  10. 17
      PacticeSolution/MVVMDataGridBinding/App.xaml.cs
  11. 10
      PacticeSolution/MVVMDataGridBinding/AssemblyInfo.cs
  12. 49
      PacticeSolution/MVVMDataGridBinding/Command/DelegateCommand.cs
  13. 14
      PacticeSolution/MVVMDataGridBinding/MVVMDataGridBinding.csproj
  14. 47
      PacticeSolution/MVVMDataGridBinding/MainWindow.xaml
  15. 28
      PacticeSolution/MVVMDataGridBinding/MainWindow.xaml.cs
  16. 22
      PacticeSolution/MVVMDataGridBinding/ViewModel/BaseViewModel.cs
  17. 70
      PacticeSolution/MVVMDataGridBinding/ViewModel/MainViewModel.cs
  18. 14
      PacticeSolution/PacticeSolution.sln

@ -23,7 +23,7 @@
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<i:Interaction.Behaviors>
<viewmodel:StudentViewModel MouseDownCommand="{Binding MouseDownCommand}"/>
<viewmodel:StudentViewModel MouseDoubleClickCommand="{Binding MouseDoubleClickCommand}"/>
</i:Interaction.Behaviors>
</DataGrid>
</StackPanel>

@ -16,7 +16,7 @@ namespace DataGridEventTriggerSample.ViewModel
{
internal class StudentViewModel : Behavior<DataGrid>, INotifyPropertyChanged
{
public static readonly DependencyProperty MouseDownProperty = DependencyProperty.Register(nameof(MouseDownCommand), typeof(ICommand), typeof(StudentViewModel), new PropertyMetadata(null));
public static readonly DependencyProperty MouseDoubleClickProperty = DependencyProperty.Register(nameof(MouseDoubleClickCommand), typeof(ICommand), typeof(StudentViewModel), new PropertyMetadata(null));
private ObservableCollection<Student> _students;
@ -53,10 +53,10 @@ namespace DataGridEventTriggerSample.ViewModel
}
public ICommand MouseDownCommand
public ICommand MouseDoubleClickCommand
{
get { return (ICommand)GetValue(MouseDownProperty); }
set { SetValue(MouseDownProperty, value); }
get { return (ICommand)GetValue(MouseDoubleClickProperty); }
set { SetValue(MouseDoubleClickProperty, value); }
}
protected virtual void OnPropertyChanged(string propertyName)
@ -65,7 +65,7 @@ namespace DataGridEventTriggerSample.ViewModel
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void OnDataGridMouseDown(object sender, MouseEventArgs e)
public void OnDataGridMouseDoubleClick(object sender, MouseEventArgs e)
{
DataGrid? dataGrid = sender as DataGrid;
if (dataGrid == null)
@ -91,12 +91,12 @@ namespace DataGridEventTriggerSample.ViewModel
protected override void OnAttached()
{
this.AssociatedObject.MouseUp += OnDataGridMouseDown;
this.AssociatedObject.MouseDoubleClick += OnDataGridMouseDoubleClick;
}
protected override void OnDetaching()
{
this.AssociatedObject.MouseUp += OnDataGridMouseDown;
this.AssociatedObject.MouseDoubleClick += OnDataGridMouseDoubleClick;
}
}
}

@ -0,0 +1,9 @@
<Application x:Class="ImageLoader.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ImageLoader"
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 ImageLoader
{
/// <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,34 @@
<Window x:Class="ImageLoader.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:ImageLoader"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<WrapPanel Grid.Column="0" Name="pnlImage"/>
</ScrollViewer>
<Canvas Grid.Column="1" Name="cvsImage"/>
</Grid>
<Label Grid.Row="0" Content="Folder: " HorizontalContentAlignment="Left" VerticalContentAlignment="Center"/>
<TextBox Grid.Row="0" x:Name="txtFolder" Width="650" Margin="10" VerticalAlignment="Center"/>
<Button Grid.Row="0" x:Name="btnFolder" Width="50" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Center" Content="Folder"
Click="btnFolder_Click"/>
<!--<StackPanel Grid.Row="1"/>-->
</Grid>
</Window>

@ -0,0 +1,104 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
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 ImageLoader
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<string> _imageList;
private List<Image> _imageControlList;
public MainWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
_imageList = new List<string>();
_imageControlList = new List<Image>();
}
private void btnFolder_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == true)
{
string fullPath = dlg.FileName;
string fileName = dlg.SafeFileName;
string path = fullPath.Replace(fileName, "");
txtFolder.Text = path;
string[] files = Directory.GetFiles(path);
_imageList = files.Where(f => f.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) >= 0
|| f.IndexOf(".png", StringComparison.OrdinalIgnoreCase) >= 0)
.ToList();
}
CreateImage(_imageList);
}
private void CreateImage(List<string> imageList)
{
for (int i = 0; i < imageList.Count; i++)
{
Image image = CreateBitmap(imageList[i]);
_imageControlList.Add(image);
image.MouseDown += Image_MouseDown;
pnlImage.Children.Add(image);
}
}
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Image image = sender as Image;
if (image == null)
return;
//WrapPanel parent = image.Parent as WrapPanel;
//if (parent != null)
// parent.Children.Clear();
Image newImage = CreateBitmap(image.Source.ToString(), 1000);
newImage.Stretch = Stretch.UniformToFill;
cvsImage.Children.Clear();
cvsImage.Children.Add(newImage);
//CreateImage(_imageList);
}
private Image CreateBitmap(string path, int widthPixel = 300)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnDemand;
bitmap.CreateOptions = BitmapCreateOptions.DelayCreation;
bitmap.DecodePixelWidth = widthPixel;
bitmap.UriSource = new Uri(path);
bitmap.EndInit();
Image img = new Image();
img.Source = bitmap;
return img;
}
}
}

@ -0,0 +1,9 @@
<Application x:Class="MVVMDataGridBinding.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMDataGridBinding"
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 MVVMDataGridBinding
{
/// <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,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MVVMDataGridBinding.Command
{
internal class DelegateCommand : ICommand
{
private readonly Func<bool> _canExecute;
private readonly Action _execute;
public event EventHandler? CanExecuteChanged;
public DelegateCommand(Action execute, Func<bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public DelegateCommand(Action execute) : this(execute, null)
{
}
public bool CanExecute(object? parameter)
{
if (_canExecute == null)
return true;
return _canExecute();
}
public void Execute(object? parameter)
{
_execute();
}
public void RaiseCanExecuteChanged()
{
if (this.CanExecuteChanged == null)
return;
this.CanExecuteChanged(this, EventArgs.Empty);
}
}
}

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Folder Include="Model\" />
</ItemGroup>
</Project>

@ -0,0 +1,47 @@
<Window x:Class="MVVMDataGridBinding.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:MVVMDataGridBinding" xmlns:viewmodel="clr-namespace:MVVMDataGridBinding.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<Window.DataContext>
<viewmodel:MainViewModel/>
</Window.DataContext>
<Window.Resources>
<Style x:Key="styDataGrid" TargetType="DataGrid">
<Setter Property="RowBackground" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalGridLinesBrush" Value="Tomato"/>
<Setter Property="VerticalGridLinesBrush" Value="Tomato"/>
<Setter Property="RowHeaderWidth" Value="0"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="MaxHeight" Value="400"/>
<Setter Property="Background" Value="White"/>
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="AutoGenerateColumns" Value="True"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" Margin="5"
Style="{StaticResource styDataGrid}"
ItemsSource="{Binding ResultTable}"/>
<Button Grid.Row="1"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Content="Update"
Command="{Binding UpdateCommand}"/>
</Grid>
</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 MVVMDataGridBinding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMDataGridBinding.ViewModel
{
internal class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged == null)
return;
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

@ -0,0 +1,70 @@
using MVVMDataGridBinding.Command;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MVVMDataGridBinding.ViewModel
{
internal class MainViewModel : BaseViewModel
{
private DataTable _resultTable;
private ICommand _updateCommand;
public DataTable ResultTable
{
get { return _resultTable; }
set
{
_resultTable = value;
OnPropertyChanged(nameof(ResultTable));
}
}
public ICommand UpdateCommand
{
get { return _updateCommand ?? (_updateCommand = new DelegateCommand(UpdateData)); }
}
public MainViewModel()
{
this.ResultTable = GetSampleData();
}
private DataTable GetSampleData()
{
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Age", typeof(int));
table.Rows.Add("Agent1", 21);
table.Rows.Add("Agent2", 22);
table.Rows.Add("Agent3", 23);
table.Rows.Add("Agent4", 24);
return table;
}
private void UpdateData()
{
this.ResultTable.Columns.Clear();
this.ResultTable.Rows.Clear();
DataTable table = new DataTable();
table.Columns.Add("Call Sign", typeof(string));
table.Columns.Add("Kill Score", typeof(int));
table.Rows.Add("Ghost", 2);
table.Rows.Add("Holyness", 4);
table.Rows.Add("Hyde", 1);
table.Rows.Add("Magellan", 5);
table.Rows.Add("Inch", 8);
this.ResultTable = table;
}
}
}

@ -23,7 +23,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MVVMCanvasRectangle", "MVVM
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MVVMButtonControl", "MVVMButtonControl\MVVMButtonControl.csproj", "{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataGridEventTriggerSample", "DataGridEventTriggerSample\DataGridEventTriggerSample.csproj", "{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataGridEventTriggerSample", "DataGridEventTriggerSample\DataGridEventTriggerSample.csproj", "{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMDataGridBinding", "MVVMDataGridBinding\MVVMDataGridBinding.csproj", "{22B968EB-0566-474B-8B1C-105E24727638}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageLoader", "ImageLoader\ImageLoader.csproj", "{F65ED213-2F48-4370-B327-FC1F4AFF1E66}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -75,6 +79,14 @@ Global
{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}.Release|Any CPU.Build.0 = Release|Any CPU
{22B968EB-0566-474B-8B1C-105E24727638}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22B968EB-0566-474B-8B1C-105E24727638}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22B968EB-0566-474B-8B1C-105E24727638}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22B968EB-0566-474B-8B1C-105E24727638}.Release|Any CPU.Build.0 = Release|Any CPU
{F65ED213-2F48-4370-B327-FC1F4AFF1E66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F65ED213-2F48-4370-B327-FC1F4AFF1E66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F65ED213-2F48-4370-B327-FC1F4AFF1E66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F65ED213-2F48-4370-B327-FC1F4AFF1E66}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save