devaness
devaness
CC#
Created by quantum on 7/24/2024 in #help
WPF DataGrid Not Updating
ObervableCollection only notifies the UI about adding and removing items, not about changing properties on items already inside the collection. You usually do not need to call PropertyChanged on an ObservableCollection yourself. Either implement INotifyPropertyChanged also for the InventoryItem class or (if you cannot or don't want to change your model class) create another viewmodel class for a single inventory item.
3 replies
CC#
Created by stigzler on 5/26/2024 in #help
Filtering an Observable Collection in WPF MVVM via ICollectionView
I did not check your whole code, but I think the problem is this line:
Gists = await gistManager.LoadGistsAsync();
Gists = await gistManager.LoadGistsAsync();
This replaces the whole list object with a new one. Without the CollectionView this works, because the view was bound directly to this property and your setter called the PropertyChanged event via the SetProperty method. When using an ObservableCollection the class already handles the PropertyChanged events itself. I usually just instantiate it once and then add or remove items from it. Omitting the setter will also prevent you from accidentally replacing the object. Like this:
public ObservableCollection<GistViewModel> Gists { get; } = new();
public ObservableCollection<GistViewModel> Gists { get; } = new();
Your LoadGistsAsync() method can return a standard list and the viewmodel can add them to the observable collection. Unfortunately ObservableCollection does not have an AddRange() method by default (though some frameworks like Prism provide one as an extension method).
var newData = await gistManager.LoadGistsAsync();
Gists.Clear();

// Gists.AddRange(newData);
foreach (var item in newData)
{
Gists.Add(item);
}
var newData = await gistManager.LoadGistsAsync();
Gists.Clear();

// Gists.AddRange(newData);
foreach (var item in newData)
{
Gists.Add(item);
}
3 replies
CC#
Created by remi.nz on 5/25/2024 in #help
✅ Help with XAML
If all of the buttons have the same size, instead of a wrap panel you could try a UniformGrid. This will stretch the elements across the width of the control. You can then change the number of columns of the grid when resizing depending on how many items currently fit in a row. Change the Width property on the buttons to MinWidth so they can expand.
<UniformGrid SizeChanged="UniformGrid_SizeChanged">
<Button MinWidth="300"/>
<Button MinWidth="300"/>
<Button MinWidth="300"/>
<Button MinWidth="300"/>
<Button MinWidth="300"/>
</UniformGrid>

private void UniformGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
UniformGrid ug = sender as UniformGrid;

double minButtonSize = 320;
int numberofButtonsPerRow = (int)(ug.ActualWidth / minButtonSize);
ug.Columns = numberofButtonsPerRow;
}
<UniformGrid SizeChanged="UniformGrid_SizeChanged">
<Button MinWidth="300"/>
<Button MinWidth="300"/>
<Button MinWidth="300"/>
<Button MinWidth="300"/>
<Button MinWidth="300"/>
</UniformGrid>

private void UniformGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
UniformGrid ug = sender as UniformGrid;

double minButtonSize = 320;
int numberofButtonsPerRow = (int)(ug.ActualWidth / minButtonSize);
ug.Columns = numberofButtonsPerRow;
}
12 replies