Hi,
try following demo as MVVM with your code:
<Window x:Class="WpfApp1.Window116"
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:WpfApp116"
mc:Ignorable="d"
Title="Chocolade-4229_230105" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid Margin="0,0,10,10">
<Label Content="Game Name" HorizontalAlignment="Left" Margin="40,59,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="GameNameTextBox" HorizontalAlignment="Left" Height="23" Margin="121,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
<Button x:Name="AddBtn" Content="Add" HorizontalAlignment="Left" Margin="121,151,0,0" VerticalAlignment="Top" Width="75" Command="{Binding}" CommandParameter="AddBtn_Click"/>
<ListView x:Name="listView" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1" ItemsSource="{Binding View}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="{Binding BackgroundColor}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="IP" DisplayMemberBinding="{Binding IP}" Width="Auto"/>
<GridViewColumn Header="PING" DisplayMemberBinding="{Binding Ping}" Width="Auto"/>
<GridViewColumn Header="Host Name" DisplayMemberBinding="{Binding DNS}" Width="Auto"/>
<GridViewColumn Header="Mac" DisplayMemberBinding="{Binding MAC}" Width="Auto"/>
<GridViewColumn Header="Výrobce" DisplayMemberBinding="{Binding Manufacturer}" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
<Button Content="Button" HorizontalAlignment="Left" Margin="101,99,0,0" VerticalAlignment="Top" Command="{Binding}" CommandParameter="Button_Click"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="31,99,0,0" VerticalAlignment="Top" Command="{Binding}" CommandParameter="Button_Click_1"/>
</Grid>
</Window>
and ViewModel:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfApp116
{
public class ViewModel : ICommand, INotifyPropertyChanged
{
private CollectionViewSource cvs = new CollectionViewSource();
private ObservableCollection<Device> col = new ObservableCollection<Device>();
public ICollectionView View { get => cvs.View; }
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public async void Execute(object parameter)
{
switch (parameter.ToString())
{
case "AddBtn_Click":
break;
case "Button_Click":
for (int i = 1; i < 20; i++)
{
Device dev = new Device() { IP = "1.1.1.1", Ping = "30ms", DNS = "XYZ", MAC = "2F:3C:5F:41:F9", Manufacturer = "Intel" };
col.Add(dev);
}
cvs.Source = col;
OnPropertyChanged(nameof(View));
break;
case "Button_Click_1":
await Main();
break;
default:
break;
}
}
Random random = new Random();
private async Task Main()
{
await Task.Run(() =>
{
// Just loop.
foreach (Device dev in col) dev.BackgroundColor = PickRandomBrush(random);
});
}
private Brush PickRandomBrush(Random rnd)
{
Brush result = Brushes.Transparent;
Type brushesType = typeof(Brushes);
PropertyInfo[] properties = brushesType.GetProperties();
int random = rnd.Next(properties.Length);
result = (Brush)properties[random].GetValue(null, null);
return result;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public class Device : INotifyPropertyChanged
{
public string IP { get; set; }
public string Ping { get; set; }
public string DNS { get; set; }
public string MAC { get; set; }
public string Manufacturer { get; set; }
private Brush _backgroundColor;
public Brush BackgroundColor
{
get => this._backgroundColor;
set
{
this._backgroundColor = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
Result: