I'm a beginner in C# and MVVM.
I have a datagrid and and I try to use a button Command to refresh it. but It doesn't work.
my XAML code
<DataGrid ItemsSource="{Binding Src_ListeApplicationARV, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="true" x:Name="Dt_ListeApplicationARV">
<Button Name="btn_synchro"
Style="{StaticResource MaterialDesignFloatingActionLightButton}"
Command="{Binding SynchroCommand}"
materialDesign:ButtonProgressAssist.IsIndicatorVisible="{Binding IsSaving}"
materialDesign:ButtonProgressAssist.IndicatorForeground="DarkGoldenrod"
materialDesign:ButtonProgressAssist.Value="{Binding SaveProgress}"
Width="35" HorizontalAlignment="Left" Margin="39,4,0,5" Cursor="Hand" Grid.Column="1" Height="35" Background="Transparent" BorderBrush="#6D92A0">
<!-- simple example of toggling/animating pack icon with a data trigger-->
<materialDesign:PackIcon Height="18" Width="19" Background="Transparent" Foreground="#6D92A0">
<materialDesign:PackIcon.Style>
<Style TargetType="materialDesign:PackIcon">
<Setter Property="Kind" Value="CloudSyncOutline" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSaveComplete}" Value="false">
<Setter Property="Kind" Value="CloudSyncOutline" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSaveComplete}" Value="True">
<Setter Property="Kind" Value="Check" />
<DataTrigger.EnterActions>
<BeginStoryboard Name="synchroboard">
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0"
To="1"
Duration="0:0:0.8" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</materialDesign:PackIcon.Style>
</materialDesign:PackIcon>
</Button>
and my code Behind
public ListeLogiciels()
{
InitializeComponent();
DataContext = new ListeLogicielsViewModel();
}
The ViewModel
public class ListeLogicielsViewModel : ViewModelBase
{
public ICommand SynchroCommand { get; }
public List<Models.ApplicationClient> Src_ListeApplicationARV
{
get { return LoadApplications(); }
set
{
Src_ListeApplicationARV = LoadApplications();
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("");
}
public ListeLogicielsViewModel()
{
var Dt_ListeApplicationARV = this.LoadApplications();// get data in a list
SynchroCommand = new AnotherCommandImplementation(_ =>
// reload dataSource
);
}
how to set my datagrid view with this Dt_ListeApplicationARV ? it's just a stupid code, and I'm blocked. thanks for your help.
I'm trying to relaod a datagrid in a viewModel.
In WPF, when you want to display a list as a ItemsSource of a control you need to use an ObservableCollection instead of a List. It's a enumerable like the List but it auto-handle the NotifyCollectionChanged event.
Replace
public List<Models.ApplicationClient> Src_ListeApplicationARV
{
get { return LoadApplications(); }
set
{
Src_ListeApplicationARV = LoadApplications();
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("");
}
By
public ObservableCollection<Models.ApplicationClient> Src_ListeApplicationARV {get; set;}
Don't forget to instantiate the ObservableCollection in the constructor.
With this, you just have to add or remove items to Src_ListeApplicationARV
in your view model and it will automatically makes changes in the UI.
The OnPropertyChanged() must be used for a property and not a collection.
Example :
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged(nameof(Name));
}
}
Sorry for my english, I'm a French developer.