Dev
Dev

Reputation: 1826

How how update `ObservableCollection<T>` Selected item?

XAML:

<ListBox ItemsSource="{Binding VentuzCollection, Mode=TwoWay}" SelectedItem="{Binding SelectedVentuzCollection, Mode=TwoWay}" ItemTemplate="{StaticResource DataTemplateVentuzCollection}" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Height="115" VerticalAlignment="Top" Width="116"/>
<ListBox ItemsSource="{Binding SelectedVentuzCollection.VentuzData, Mode=TwoWay}" HorizontalAlignment="Left" Height="126" VerticalAlignment="Top" Width="122" Margin="168,164,0,0" ItemTemplate="{StaticResource DataTemplateVentuzDataCollection}" />

C#:

private static ObservableCollection<VentuzSocialManager> _ventuzCollection;
public ObservableCollection<VentuzSocialManager> VentuzCollection
{
    get => _ventuzCollection;
    set
    {
        if (_ventuzCollection == value) return;
        _ventuzCollection = value;
        OnPropertyChanged();
    }
 }

private static VentuzSocialManager _selectedventuzCollection;
public VentuzSocialManager SelectedVentuzCollection
{
    get => _selectedventuzCollection;
    set
    {
        if (_selectedventuzCollection == value) return;
        _selectedventuzCollection = value;
        OnPropertyChanged();
    }
 }

public class VentuzSocialManager:ViewModelBase
{
    public  string Id { get; set; }
    private static ObservableCollection<VentuzSocial> _ventuzData;
    public ObservableCollection<VentuzSocial> VentuzData
    {
        get => _ventuzData;
        set
        {
            if (_ventuzData == value) return;
            _ventuzData = value;
            OnPropertyChanged();
        }
    }
}

I used VentuzDataViewModel.This.SelectedVentuzCollection?.VentuzData?.Add(exportSocial); to update the selected item but when I use this statement it updates all objects in VentuzCollection. What I am trying to do is when an item is selected in the first list box whatever I add to the second Listbox gets updated in only the selected item of the first Listbox.

Upvotes: 0

Views: 330

Answers (1)

Rekshino
Rekshino

Reputation: 7325

You have to remove static from

private static ObservableCollection<VentuzSocial> _ventuzData;

if you want to have one instance of ObservableCollection<VentuzSocial> for every VentuzSocialManager object, otherwise you will have, what you have - one instance of ObservableCollection<VentuzSocial> for all VentuzSocialManager objects.

Upvotes: 1

Related Questions