Pan*_*fas 5 silverlight binding path itemssource staticresource
我有一个名为Customer2个属性的简单类.
然后我创建了一个只用一个名为Customers的公共属性命名的集合类
public Name {get;set;}
public LastName {get;set}CustomerList
public class CustomerList
{
public List<Customer> Customers { get; set; }
public CustomerList()
{
Customers = new List<Customer>();
Customers.Add(new Customer() { Name = "Foo", LastName = "Bar" });
Customers.Add(new Customer() { Name = "Foo1", LastName = "Bar1" });
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在XAML中将此类用作静态资源.
<UserControl.Resources>
<customers:CustomerList x:Key="CustomersKey">
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
然后在ListBox中使用它
<ListBox x:Name="lvTemplate" ItemsSource="{Binding Source={StaticResource CustomersKey}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Name}"/>
<TextBox Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
如果我在代码中设置ItemsSource behide,在实例化类之后,一切正常.如果我尝试从XAML设置它并且静态资源没有发生.即使我使用{Binding Path=Customer.Name}或者{Binding Path=Name}.
显然我想念一些......
由于CustomerList实际上不是项目列表(不实现IEnumerable),因此您需要指定要用作ItemsSource的对象内的属性.
<ListBox ItemsSource="{Binding Path=Customers, Source={StaticResource CustomersKey}}">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4102 次 |
| 最近记录: |