0

I'm trying to do something pretty simple, but I'm not super experienced on WPF: I want to click on an item in a list view, popup a dialog that will delete or update that item, then refresh the list, without calling up the delete/edit dialog again.

Here's the code I'm working with: XAML:

 <ListView x:Name="peopleListView"
              Height="280"
              Margin="0,5,0,0"
              SelectionChanged="peopleListView_SelectionChanged">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>

                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <uc:PersonControl PersonDetails="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

The codebehind is this:

 private void peopleListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        PersonDetails selectedPersonDetails = (PersonDetails) peopleListView.SelectedItem;

        EditAndDeletePeopleWindow editAndDelPeople = new EditAndDeletePeopleWindow(selectedPersonDetails);
        editAndDelPeople.ShowDialog();
        DisplayPeopleFromDatabase();
    }

The elements of the list are a control. Every time the DisplayPeopleFromDatabase runs, the SelectionChanged even fires, which calls up the edit dialog.

Here's the DisplayPeopleFromDatabase code:

  private void DisplayPeopleFromDatabase()
    {
       
        List<PersonDetails> personDetailList = new List<PersonDetails>(dbPlanningService.GetAllPeopleList());

        if (personDetailList != null)
        {
            peopleListView.ItemsSource = personDetailList;
        }
       
    }
Cynon
  • 115
  • 10
  • add bool field like isRefreshing to change your SelectionChanged code flow (off-topic: also for windows users is more common to use double click for edit) – Selvin Apr 08 '21 at 00:35
  • Don't use `SelectionChanged` to trigger the dialog. Respond to the mouse itself. See duplicate. – Peter Duniho Apr 08 '21 at 00:40

0 Answers0