I'm trying to make a BubbleSort visualizer in a WPF project and every time I perform a swap the UI is supposed to refresh becouse of this code
public ObservableCollection<MyDataObject> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged(nameof(Items));
}
}
Here is the full MainViewModel class
using SaleWPF;
using SaleWPF.FrameWork;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Windows.Input;
using System.Threading.Tasks;
using System.Windows;
namespace SortVisualizer
{
public class MainViewModel : BaseNotification, INotifyPropertyChanged
{
public ICommand SortCommand { get; set; }
public MainViewModel()
{
// Inizializza la collezione di oggetti con i dati che devono essere visualizzati
Items = new ObservableCollection<MyDataObject>
{
new MyDataObject { Name = "Nome 1", Value = "Valore 1", Altezza = 30 },
new MyDataObject { Name = "Nome 2", Value = "Valore 2" ,Altezza = 40},
new MyDataObject { Name = "Nome 3", Value = "Valore 3" , Altezza = 50},
new MyDataObject { Name = "Nome 3", Value = "Valore 3" , Altezza = 60},
new MyDataObject { Name = "Nome 3", Value = "Valore 3" , Altezza = 70},
new MyDataObject { Name = "Nome 3", Value = "Valore 3" , Altezza = 80},
new MyDataObject { Name = "Nome 3", Value = "Valore 3" , Altezza = 90},
new MyDataObject { Name = "Nome 3", Value = "Valore 3" , Altezza = 100},
new MyDataObject { Name = "Nome 3", Value = "Valore 3" , Altezza = 110},
};
// Imposta il titolo della vista
Title = "Esempio di StackPanel con Binding";
SortCommand = new RelayCommand(Sort);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}
private ObservableCollection<MyDataObject> _items;
public ObservableCollection<MyDataObject> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged(nameof(Items));
}
}
public async void Sort()
{
bool flag = true;
int count = 0;
for (int i = 1; (i <= (Items.Count - 1)) && flag; i++)
{
flag = false;
for (int j = 0; j < (Items.Count - 1); j++)
{
count = count + 1;
if (Items[j + 1].Altezza > Items[j].Altezza)
{
var temp = Items[j];
Items[j] = Items[j + 1];
Items[j + 1] = temp;
flag = true;
}
}
}
}
public class MyDataObject
{
public string Name { get; set; }
public string Value { get; set; }
public int Altezza { get; set; }
}
}
}
And this is the full XAML code
<Window x:Class="SortVisualizer.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:SortVisualizer"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:MainViewModel x:Key="ViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource ViewModel}">
<StackPanel Height="434" VerticalAlignment="Top">
<Button Command="{Binding SortCommand}" Content="Sorta"></Button>
<TextBlock Text="{Binding Title}"></TextBlock>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Width="{Binding Altezza}" Fill="Black" Height="10">
<Rectangle.RenderTransform>
<TranslateTransform X="10" />
</Rectangle.RenderTransform>
</Rectangle>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</Window>
The sort works but the page refreshed at the end only
I tried to insert a Thread.Sleep inside the bubble sort code but there is no difference. I even tried to insert this line of code inside of the bubble sort
Title = Title+"i";
so that the propriety title will make the page refresh but it did not work either.