JackyBoi
JackyBoi

Reputation: 2773

WPF creating a list

I have a collection of strings in C# MAINWINDOW.xaml.cs like this

public class NameList : ObservableCollection<Shortcuts>
        {
            public NameList()     : base()
            {
                Add(new Shortcuts("ctrl", "b","s"));
                Add(new Shortcuts("ctrl", "b","m"));
                Add(new Shortcuts("ctrl", "b","p"));
                Add(new Shortcuts("ctrl", "b","1"));
            }
        }

        public class Shortcuts
        {
            private string firstkey;
            private string secondkey;
            private string lastkey;

            public Shortcuts(string first, string second, string last)
            {
                this.firstkey = first;
                this.secondkey = second;
                this.lastkey= last;
            }

            public string Firstkey
            {
                get { return firstkey; }
                set { firstkey = value; }
            }

            public string Secondkey
            {
                get { return secondkey; }
                set { secondkey = value; }
            }

            public string Lastkey
            {
                get { return lastkey; }
                set { lastkey = value; }
            }
        }
    }

Then in the MainWindow.xaml itself I have a combobox and I would like to bind these items to the combobox so this is what I did

<Window x:Class="Testing_learning.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:Testing_learning"
        Title="Outlook Context Settings" Height="350" Width="525">
    <Window.Resources>
         <c:ListName Key ="NameListData"/>

    </Window.Resources>

oh BTW the project name is Testing_learning that is why there is this

xmlns:c="clr-namespace:Testing_learning"

BUT the problem is that when i add this line of code

<Window.Resources>
         <c:ListName Key ="NameListData"/>

        </Window.Resources>

I get an error at c:ListName the type ListName was not found why is this? Any thoughts?

Screenshot 1 enter image description here

Screenshot 2 enter image description here

Upvotes: 0

Views: 3735

Answers (4)

LPL
LPL

Reputation: 17063

You have one more typo: Key has to be x:Key

<Window.Resources>
    <c:NameList x:Key="NameListData" />
</Window.Resources>

and you have nested your classes in MainWindow class. Move it out:

namespace Testing_learning
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class NameList : ObservableCollection<Shortcuts>
    {
        ...
    }

    public class Shortcuts
    {
        ...
    }
}

Upvotes: 1

Lescai Ionel
Lescai Ionel

Reputation: 4373

When I moved the two classes to separate files it worked.

EDIT:

Building also fixes the problem even if the classes are in MainWindow.xaml

enter image description here

Upvotes: 0

raveturned
raveturned

Reputation: 2677

Your class definition:

public class NameList : ObservableCollection<Shortcuts>
    {
    //[…]

Your xaml:

<c:ListName Key ="NameListData"/>

NameList is different to ListName. Since you don't have a ListName class you'll see the "ListName was not found" error.

Upvotes: 2

JRadness
JRadness

Reputation: 304

It appears to be a typo? Your C# has NameList, but your XAML has ListName.

Upvotes: 2

Related Questions