简体   繁体   English

WPF ObservableCollection不会绑定到ListBox

[英]WPF ObservableCollection won't bind to ListBox

I have an ObservableCollection as a member of my class. 我有一个ObservableCollection作为班级成员。 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. 从“ Debug输出中可以看到,有3个项目加载到我的Observable集合中。

Then the XAML looks like this: 然后,XAML如下所示:

<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. 我对此感到非常困惑,因为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. 我是C#和WPF和Windows的新手,试图弄清楚如何对现有代码进行更改。 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. 其次,您很可能是DataContext错误。 You need to figure out what the DataContext is of the control your ListBox is in. My guess is that the DataContext is either: 您需要弄清楚ListBox所在ListBoxDataContext是什么。我的猜测是DataContext是:

  1. Your DataContext is not set at all 您的DataContext根本没有设置
  2. UiPageData is your DataContext meaning you need to do ItemsSource="{Binding SavedMachines}" UiPageData是您的DataContext这意味着您需要做ItemsSource="{Binding SavedMachines}"
  3. Your DataContext is set to something totally different, and we can't really help you with that 您的DataContext设置为完全不同的内容,我们真的无法为您提供帮助

You should use tools like Visual Studio's Output window and/or Snoop to figure out your binding issues. 您应该使用Visual Studio的“ Output窗口和/或“ Snoop来找出绑定问题。

Also, an ObservableCollection<string> seems a little useless. 另外, ObservableCollection<string>似乎没有用。 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. 我建议您在某个时候进行public ObservableCollection<DataManagement.Machines> SavedMachines ,然后指定ListBox.ItemTemplate以使用一个DataTemplate来以您希望的方式显示DataManagement.Machines对象。

Your SavedMachines implementation looks wrong. 您的SavedMachines实现看起来不对。

you should be adding to SavedMachines instead of _SavedMachines 您应该添加到SavedMachines而不是_SavedMachines

ObservableCollection doesnt need OnPropertyChanged. ObservableCollection不需要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: 您的ObservableCollection应该看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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