Replies: 3 comments 3 replies
-
The item data is carried within the <DataGrid ItemsSource="{Binding YourItemsSource}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ColumnContent}" />
</DataGrid.Columns>
<DataGrid.Styles>
<!-- Select all DataGridRow via Style -->
<Style Selector="DataGridRow">
<!-- Set the DataGridRow.IsVisible property to the value of the IsVisible property of the data item -->
<Setter Property="IsVisible" Value="{Binding (ns:ItemDataType).IsVisible}" />
</Style>
</DataGrid.Styles>
</DataGrid>
To notify the view (binding system) of property changes, the class containing the properties needs to implement the using System.ComponentModel;
public class ItemDataType : INotifyPropertyChanged
{
private bool _isVisible;
/// <remarks>
/// This property does not implement the INotifyPropertyChanged interface event invoking,
/// hence it will not be updated in the UI.
/// </remarks>
public required string ColumnContent { get; set; }
/// <remarks>
/// This property implements the INotifyPropertyChanged interface event invoking,
/// hence it will be updated in the UI.
/// </remarks>
public required bool IsVisible
{
get => _isVisible;
set => SetField(ref _isVisible, value);
}
/// <inheritdoc />
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Notify property changed.
/// </summary>
/// <param name="propertyName"> The property name. </param>
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Set field value and notify property changed.
/// </summary>
/// <param name="field"> The field. </param>
/// <param name="value"> The value. </param>
/// <param name="propertyName"> The property name. </param>
/// <typeparam name="T"> The field type. </typeparam>
/// <returns> True if the field value is changed; otherwise, false. </returns>
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
} If you are using ReactiveUI or CommunityToolkit.Mvvm, codes above can often be significantly simplified.
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
public class ItemDataType : ReactiveObject
{
[Reactive]
public bool IsVisible { get; set; }
}
using CommunityToolkit.Mvvm.ComponentModel;
public partial class ItemDataType : ObservableObject
{
[ObservableProperty]
private bool _isVisible;
} Additionally, if you need to notify the UI of changes to the members of the same collection object reference instead of replacing the object instance of a collection property, the collection needs to implement the |
Beta Was this translation helpful? Give feedback.
-
Removing item from the observable list should be the most reliable way to do it. I don't know if IsVisible solution works, but you can try it, if it suits you better. |
Beta Was this translation helpful? Give feedback.
-
I implemented something with
This was working, however upon adding features, the DataGrid Source changes when a the concept of a 'Profile' is selected, essentially changing the The visibility logic is calculated in-real based on a running application doing calculations each second, the list of data is small, but the style seemed like the most obvious choice to me, since the ones 'not visible' are ViewModelItems themself which are performing some calculations and the 'IsVisible' could become true. Not sure what my options are now that this style seems to break when re-binding from different Observable Collections (each profile is its own list of the same ViewModel items). I thought it would have been nice if |
Beta Was this translation helpful? Give feedback.
-
Hi,
in my project I have to hide rows in a DataGrid according to some criteria.
My current approach is to keep the full set of data in a List<> and provide a filtered List<> as Source property for the DataGrid.
I am now wondering how to notify the DataGrid about changes in the Lists.
I tried several approaches to update the view after refreshing the filtered list, nothing worked. RaisePropertyChanged will not work since it only is for changes of single properties of MyClass and to update the binded cell. The filter property is not shown in the DataGrid and should affect the whole row and not only a single cell.
My issue is about two questions:
Beta Was this translation helpful? Give feedback.
All reactions