简体   繁体   中英

Refesh listbox from xaml

In my WPF MVVM application I am currently trying to move from button event handlers to Commands.

I have a list box that represents the list of contacts, this data is bound to my viewmodel.

<ListBox Grid.Row="1" Grid.Column="0" x:Name="ContactListBox" Margin="5" BorderThickness="2"
      DisplayMemberPath="Name"
      IsSynchronizedWithCurrentItem="True"
      SelectionMode="Single">
          <ListBox.Style>
                <Style TargetType="ListBox">
                    <Style.Triggers>
                         <DataTrigger Binding="{Binding IsShowingFavorites}" Value="True">
                               <Setter Property="ItemsSource" Value="{Binding Path=FavContactList}" />
                               <Setter Property="SelectedItem" Value="{Binding Path=CurrentFavContact}" />
                          </DataTrigger>
                          <DataTrigger Binding="{Binding IsShowingFavorites}" Value="False">
                               <Setter Property="ItemsSource"  Value="{Binding Path=ContactList}" />
                               <Setter Property="SelectedItem" Value="{Binding Path=CurrentContact}" />
                          </DataTrigger>
                   </Style.Triggers>
               </Style>
         </ListBox.Style>
</ListBox> 

I also have a button that adds new contact:

<ToggleButton x:Name="AddFavs" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0" Click="AddFavs_Click" >

Right now I want to get rid of my AddFavs_Click

    /// <summary>
    /// Handler for Add click event
    /// </summary>
    /// <param name="sender">Not used</param>
    /// <param name="e">Not used</param>
    private void AddFavs_Click(object sender, RoutedEventArgs e)
    {
        _viewModel.AddFavorites();
        ContactListBox.Items.Refresh();
    }

with AddFavourites command. So far I can move AddFavorites(); to be a command, however, my view model doesn't have access to the view, so I am unable to do ContactListBox.Items.Refresh(); in my view model, and that's why when new Contact is added, ContactListBox is not being updated properly.

My guess was to somehow trigger Item.Refresh() from XAML, but I haven't found solution yet.

So many times I have seen a WPF developer trying to save on XAML and implementing some ridiculous data-sharing view or control and then coming here and saying what's happening here? . You're all making your life much more difficult to save on one small XAML file?? I'm sure you'll say that that's not what you're doing, but either way, my advice would be the same.

Don't share views with different view models and especially don't share UI controls with different data sources. If you were really writing an application using the MVVM pattern, then as @sthotakura mentioned, you wouldn't need to update the UI at all... you'd make a change to the data and the UI would automatically update , thanks to the INotifyPropertyChanged interface.

You'll also find it a lot simpler hiding and showing additional DataGrid controls that are already set up in XAML then trying to change the data bound sources at run time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM