outlook email
outlook email

Reputation: 361

Wpf ListView trigger SourceUpdate

How do i trigger the SourceUpdate of my wpf listview?

This is my xaml

<ListView DataContext="{StaticResource vmInstance}" SelectedItem="{Binding selectedItem}" BorderBrush="Beige" BorderThickness="1" ItemsSource="{Binding lstExternal}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged" >
            <cmd:EventToCommand Command="{Binding Path=ItemChanged}"
                            PassEventArgsToCommand="True"  />
        </i:EventTrigger>
        <i:EventTrigger EventName="SourceUpdated">
            <cmd:EventToCommand Command="{Binding Path=SourceUpdated}"
                            PassEventArgsToCommand="True"  />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListView.View >                     
    </ListView.View>
</ListView>

ViewModel:

public static RelayCommand SourceUpdated { get; set; }

//on initialize
SourceUpdated = new RelayCommand(SourceUpdateEvent);

public void SourceUpdateEvent()
{
    Console.WriteLine("Updated");
}

Why does SourceUpdateEvent doesn't run?

Am I using the wrong event?

Thank you

Upvotes: 0

Views: 667

Answers (1)

mm8
mm8

Reputation: 169420

want to execute a method when the itemsource of the listview is changed or updated

Then bind the ItemsSource property of the ListView to a source property of your view model

<ListView ItemsSource="{Binding YourCollection}" ...>

...and invoke your command in the setter of this source property:

private ObservableCollection<YourType> _sourceCollection;
public ObservableCollection<YourType> SourceCollection
{
    get { return _sourceCollection; }
    set
    {
        _sourceCollection = value;
        RaisePropertyChanged();
        SourceUpdated.Execute(null);
    }
}

This is the (only) correct way to solve this using the MVVM pattern.

Upvotes: 1

Related Questions