canvas rectangle generate

main
syneffort 2 years ago
parent 0ec24ba4e1
commit 2d572bcb1b
  1. 9
      PacticeSolution/MVVMCanvasRectangle/App.xaml
  2. 17
      PacticeSolution/MVVMCanvasRectangle/App.xaml.cs
  3. 10
      PacticeSolution/MVVMCanvasRectangle/AssemblyInfo.cs
  4. 10
      PacticeSolution/MVVMCanvasRectangle/MVVMCanvasRectangle.csproj
  5. 35
      PacticeSolution/MVVMCanvasRectangle/MainWindow.xaml
  6. 64
      PacticeSolution/MVVMCanvasRectangle/MainWindow.xaml.cs
  7. 16
      PacticeSolution/MVVMCanvasRectangle/Model/RectItem.cs
  8. 48
      PacticeSolution/MVVMCanvasRectangle/ViewModel/RectModelView.cs
  9. 6
      PacticeSolution/PacticeSolution.sln

@ -0,0 +1,9 @@
<Application x:Class="MVVMCanvasRectangle.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MVVMCanvasRectangle"
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 MVVMCanvasRectangle
{
/// <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,35 @@
<Window x:Class="MVVMCanvasRectangle.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:MVVMCanvasRectangle" xmlns:viewmodel="clr-namespace:MVVMCanvasRectangle.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="600">
<Window.DataContext>
<viewmodel:RectModelView/>
</Window.DataContext>
<StackPanel>
<TextBox x:Name="txtCount" HorizontalAlignment="Center" Margin="5" Width="100" Text="{Binding Count, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<ItemsControl x:Name="itcMain" ItemsSource="{Binding RectItems}" Height="150" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="LightCoral"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Window>

@ -0,0 +1,64 @@
using MVVMCanvasRectangle.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
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;
using System.Windows.Threading;
namespace MVVMCanvasRectangle
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private System.Timers.Timer _timer;
public MainWindow()
{
InitializeComponent();
_timer = new System.Timers.Timer();
_timer.Interval = 1000;
_timer.Elapsed -= _timer_Elapsed;
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
}
private void _timer_Elapsed(object? sender, ElapsedEventArgs e)
{
if (this.Dispatcher.Thread != Thread.CurrentThread)
{
this.Dispatcher.Invoke(DispatcherPriority.Normal, () =>
{
AddOneRect();
});
}
else
{
AddOneRect();
}
}
private void AddOneRect()
{
RectModelView dataContext = this.DataContext as RectModelView;
if (dataContext == null)
return;
dataContext.AddOne();
}
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMCanvasRectangle.Model
{
internal class RectItem
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
}
}

@ -0,0 +1,48 @@
using MVVMCanvasRectangle.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMCanvasRectangle.ViewModel
{
internal class RectModelView : INotifyPropertyChanged
{
private int _count;
public ObservableCollection<RectItem> RectItems { get; set; }
public event PropertyChangedEventHandler? PropertyChanged;
public int Count
{
get { return _count; }
set
{
_count = value;
this.RectItems.Clear();
for (int i = 0; i < _count; i++)
{
this.RectItems.Add(new RectItem { X = i * 40, Y = 10, Width = 30, Height = 30 });
}
}
}
public RectModelView()
{
_count = 0;
this.RectItems = new ObservableCollection<RectItem>();
}
public void AddOne()
{
RectItem lastRect = this.RectItems.LastOrDefault();
if (lastRect == null)
this.RectItems.Add(new RectItem() { X = 0, Y = 10, Width = 30, Height = 30 });
else
this.RectItems.Add(new RectItem() { X = lastRect.X + 40, Y = 10, Width = 30, Height = 30 });
}
}
}

@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MVVMDatabaseSample", "MVVMD
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMTextInputDetection", "MVVMTextInputDetection\MVVMTextInputDetection.csproj", "{C55A5081-B142-4120-8F62-3991276EF5C2}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMTextInputDetection", "MVVMTextInputDetection\MVVMTextInputDetection.csproj", "{C55A5081-B142-4120-8F62-3991276EF5C2}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMCanvasRectangle", "MVVMCanvasRectangle\MVVMCanvasRectangle.csproj", "{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -57,6 +59,10 @@ Global
{C55A5081-B142-4120-8F62-3991276EF5C2}.Debug|Any CPU.Build.0 = Debug|Any CPU {C55A5081-B142-4120-8F62-3991276EF5C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C55A5081-B142-4120-8F62-3991276EF5C2}.Release|Any CPU.ActiveCfg = Release|Any CPU {C55A5081-B142-4120-8F62-3991276EF5C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C55A5081-B142-4120-8F62-3991276EF5C2}.Release|Any CPU.Build.0 = Release|Any CPU {C55A5081-B142-4120-8F62-3991276EF5C2}.Release|Any CPU.Build.0 = Release|Any CPU
{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save