Why am I getting a “type reference cannot find public type” in this data binding?

Why does ObjectDataProvider not repeat the classification of "local: Customer" in this example?

When i type

<local:

I get intellisense for "Customer", so it should work. In this example, I have no code.

XAML:

<Window x:Class="TestDataTemplate124.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestDataTemplate124"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="Customers"
                            ObjectType="x:Type local:Customer"
                            MethodName="GetAllCustomers"/>
    </Window.Resources>
    <StackPanel>
        <ListBox DataContext="{StaticResource Customers}" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}"/>
                        <TextBlock Text=" "/>
                        <TextBlock Text="{Binding LastName}"/>
                        <TextBlock Text=" ("/>
                        <TextBlock Text="{Binding Age}"/>
                        <TextBlock Text=")"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

Customer.cs:

using System.Collections.ObjectModel;

namespace TestDataTemplate124
{
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

        public static ObservableCollection<Customer> GetAllCustomers()
        {
            ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
            customers.Add(new Customer() { FirstName = "Jim", LastName = "Smith", Age = 23 });
            customers.Add(new Customer() { FirstName = "John", LastName = "Jones", Age = 22 });
            customers.Add(new Customer() { FirstName = "Jay", LastName = "Anders", Age = 21 });
            return customers;
        }
    }
}
+3
source share
2 answers

"x: Type" is a markup extension, so enclose it in braces:

ObjectType="{x:Type local:Customer}"
+1
source

Try adding a namespace as a prefix for the class.

0
source

Source: https://habr.com/ru/post/1707184/


All Articles