C# - WPF class get all resources and fill listbox

I have class who get all resource names. Here is code :

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Helper
{

    public static class callResources
    {

        public static ObservableCollection<string> callAllResources()
        {
            var properties = typeof(Properties.Resources).GetProperties(
      System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
      System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static);

            ObservableCollection<string> col = new ObservableCollection<string>();
            foreach (var propertyInfo in properties)
            {
                var s = propertyInfo.Name;
                if (s != "ResourceManager" && s != "Culture")
                {
                    col.Add(s);
                }
            }
           return col;
        }
    }
}

in MainWindows.cs have this code:

public ObservableCollection<string> resourcesListBox
        {
            get
            {
                return callResources.callAllResources();
            }
        }

If I call resourcesListBox I have only one line in queryListBox.

and in MainWindow.xaml have this but it does not work :

 <ListBox x:Name="queryListBox" HorizontalAlignment="Left" Height="155" Margin="18,10,0,0" SelectionMode="Single" VerticalAlignment="Top" Width="389"  ItemsSource="{Binding Path = resourcesListBox}" SelectionChanged="listBox_SelectionChanged"/>

Upvotes: 0

Views: 689

Answers (2)

Thomas V
Thomas V

Reputation: 151

So first you need to make your class static so you can call it from your code behind without creating a new instance.

Secondly you need to return something like a list or a ObservableCollection in your callAllResources method and place all the names in this list or ObservableCollection.

then you can call this method from your code behind and it returns a list or ObservableCollection of resource names.

callResources class:

public static class callResources
  {

    public static ObservableCollection<string> callAllResources()
    {
      var properties = typeof(Properties.Resources).GetProperties(
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static);

      ObservableCollection<string> col = new ObservableCollection<string>();
      foreach (var propertyInfo in properties)
      {
        var s = propertyInfo.Name;
        if (s != "ResourceManager" && s != "Culture")
        {

          col.Add(s);

        }
      }
      return col;
    }
  }

code behind:

public ObservableCollection<string> resourcesListBox
{
  get
  {
    return callResources.callAllResources();
  }
}

Xaml code:

<ListBox x:Name="Listbox1" ItemsSource="{Binding Path=resourcesListBox}"/>

Upvotes: 1

Barr J
Barr J

Reputation: 10927

Try this way:

stringList.ForEach(item => listBox1.Items.Add(item))

Upvotes: 1

Related Questions