在此示例中,为什么ObjectDataProvider无法识别“ local:Customer”?
当我打字<local:
我获得“客户”的智能感知,因此它应该可以工作。在此示例中,我没有任何代码隐藏。
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;
}
}
}
最佳答案
“ x:Type”是标记扩展,因此将其括在花括号中:
ObjectType="{x:Type local:Customer}"
关于c# - 为什么在此数据绑定(bind)上出现“类型引用找不到公共(public)类型”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/793102/