简体   繁体   中英

WPF ObservableCollection won't bind to ListBox

I have an ObservableCollection as a member of my class. This observable collection is filled from a database when a button is clicked which reveals the next screen, and then I would like all of the items in the observable collection to be displayed in a list box.

Here is my code:

//Inside My UiPageData class
public ObservableCollection<string> _SavedMachines = new ObservableCollection<string>();

    public ObservableCollection<string> SavedMachines
    {
        get { return _SavedMachines; }

        set
        {
            _SavedMachines.Add(value.ToString());
            NotifyPropertyChanged("SavedMachines");
        }
    }  

public void EventClick()
    {
        if (Instance.CurrentPage != UiPageEnum.eVechWizardScreen)
        {
            Instance.AdvancedSettings.CurrentWizardScreen = VehicleWizardEnum.eLoadVehicle;

            DataManager vechDM = DataManager.getDataManager();
            vechDM.close();
            vechDM.open("Path to DB");

            DataManagement.Machines[] machs = vechDM.getMachines();
            if (machs.Length > 0)
            {
                _SavedMachines.Clear();
                for (int i = 0; i < machs.Length; i++)
                {
                    _SavedMachines.Add(machs[i].Name);
                    Debug.WriteLine(machs[i].Name);
                }

            }
        }
        Instance.CurrentPage = UiPageEnum.eVechWizardScreen;
    }

I can see from the Debug output that there are 3 items loaded into my Observable Collection.

Then the XAML looks like this:

<TextBlock Text="Current Vehicles"
            Style="{StaticResource HeaderStyle}" />
<ListBox Grid.Row="1"
            Grid.ColumnSpan="2"
            ItemsSource="{Binding UiPageData.SavedMachines}">
    <ListBox.Resources>
        <Style TargetType="ListBox"
                BasedOn="{StaticResource WgListBox}">
            <Setter Property="ListBox.IsEnabled"
                    Value="True" />
        </Style>
    </ListBox.Resources>
</ListBox>

I'm getting very confused with this, as nothing shows up in the ListBox. I'm a newcomer to C# and WPF and Windows in general, trying to figure out how to make the changes I need to make to existing code. Any help would be gratefully received.

For one, do this:

private ObservableCollection<string> _SavedMachines = new ObservableCollection<string>();
public ObservableCollection<string> SavedMachines
{
    get { return _SavedMachines; }
}

Secondly, you're DataContext is more than likely wrong. You need to figure out what the DataContext is of the control your ListBox is in. My guess is that the DataContext is either:

  1. Your DataContext is not set at all
  2. UiPageData is your DataContext meaning you need to do ItemsSource="{Binding SavedMachines}"
  3. Your DataContext is set to something totally different, and we can't really help you with that

You should use tools like Visual Studio's Output window and/or Snoop to figure out your binding issues.

Also, an ObservableCollection<string> seems a little useless. I would recommend at some point you do public ObservableCollection<DataManagement.Machines> SavedMachines and then specify your ListBox.ItemTemplate to use a DataTemplate that shows the DataManagement.Machines object the way you want it to show.

Your SavedMachines implementation looks wrong.

you should be adding to SavedMachines instead of _SavedMachines

ObservableCollection doesnt need OnPropertyChanged. The implementation of your property should have been simply

public ObservableCollection<string> SavedMachines{get;set;}

I agree with Krishna. Your ObservableCollection should look like this:

protected ObservableCollection<string> _SavedMachines = new ObservableCollection<string>();
public ObservableCollection<string> SavedMachines
{
    get { return _SavedMachines; }
    set
    {
        if (_SavedMachines != value) 
        {
          _SavedMachines = value;
          NotifyPropertyChanged("SavedMachines");
        }
    }
}  

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