Silverlight Datagrid的有趣问题
各位 我在Silverlight DataGrid数据绑定方面遇到了一个有趣的问题。可能是b/c我没有正确绑定数据源。这是对象&可观察的集合Silverlight Datagrid的有趣问题,silverlight,datagrid,Silverlight,Datagrid,各位 我在Silverlight DataGrid数据绑定方面遇到了一个有趣的问题。可能是b/c我没有正确绑定数据源。这是对象&可观察的集合 /// <summary> /// Interface for all model elements /// </summary> public interface IBaseModel { } /// <summary> /// Employee model /// </summary> public
/// <summary>
/// Interface for all model elements
/// </summary>
public interface IBaseModel
{
}
/// <summary>
/// Employee model
/// </summary>
public class EmployeeModel : IBaseModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return FirstName + LastName;
}
}
// The observable collection is loaded and bound in the user control
public partial class EmployeeMasterDetailsWindow : UserControl
{
public EmployeeMasterDetailsWindow()
{
try
{
InitializeComponent();
ObservableCollection<IBaseModel> k = new ObservableCollection<IBaseModel>()
{new EmployeeModel(){FirstName="Frodo",
LastName=" Baggins"},
new EmployeeModel(){FirstName="Pippin",
LastName="Thomas"},
new EmployeeModel(){FirstName="John",
LastName="Doe"},
new EmployeeModel(){FirstName="Tim",
LastName="Kiriev"}};
dataGrid1.DataContext = k;
CustomersListBox.DataContext = k;
}
catch (Exception ex)
{
}
}
}
//here's the XAML
<UserControl x:Class="AdventureWorksManagement.UI.EmployeeMasterDetailsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="379" d:DesignWidth="516"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">
<UserControl.Resources>
<DataTemplate x:Key="CustomerTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding LastName}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" Height="371" Width="595">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="312*" />
<ColumnDefinition Width="283*" />
</Grid.ColumnDefinitions>
<sdk:DataGrid Height="325" HorizontalAlignment="Left"
Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="271" ItemsSource="{Binding}"
RowDetailsTemplate="{StaticResource CustomerTemplate}">
</sdk:DataGrid>
<ListBox x:Name="CustomersListBox"
Margin="10,10,10,11"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource CustomerTemplate}" />
</Grid>
//
///所有模型元素的接口
///
公共接口IBaseModel
{
}
///
///员工模式
///
公共类EmployeeModel:IBaseModel
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共重写字符串ToString()
{
返回FirstName+LastName;
}
}
//在用户控件中加载并绑定可观察集合
公共部分类EmployeeMasterDetailsWindow:UserControl
{
公共雇员MasterDetailsWinDow()
{
尝试
{
初始化组件();
ObservableCollection k=新的ObservableCollection()
{new EmployeeModel(){FirstName=“Frodo”,
LastName=“Baggins”},
新建EmployeeModel(){FirstName=“Pippin”,
LastName=“Thomas”},
新建EmployeeModel(){FirstName=“John”,
LastName=“Doe”},
新建EmployeeModel(){FirstName=“Tim”,
LastName=“Kiriev”};
dataGrid1.DataContext=k;
CustomerListBox.DataContext=k;
}
捕获(例外情况除外)
{
}
}
}
//这是XAML
列表框显示所有员工,但DataGrid不显示。我甚至没有看到数据网格。我在输出窗口中看到此错误消息:
'System.Collections.ObjectModel.ObservableCollection1[AdventureWorksManagement.Model.IBaseModel]'
'System.Collections.ObjectModel.ObservableCollection
1[AdventureWorksManagement.Model.IBaseModel]'
(HashCode=54025633)。
BindingExpression:Path='FirstName'
DataItem='System.Collections.ObjectModel.ObservableCollection'1[AdventureWorksManagement.Model.IBaseModel]'
(HashCode=54025633);目标元素是
'System.Windows.Controls.TextBlock'
(名称=“”);目标属性为“Text”
(输入'System.String')
我可能做错了什么?通过将其设置为
可观察集合
可以有效地将所有子对象强制转换为IBaseModel,而IBaseModel没有成员
在本例中,将其设置为一个
可观察集合
列表框是否显示FirstName+LastName而不是您在CustomerTemplate中声明的内容,但列表框控件如何在不进行强制转换的情况下显示可观察集合中的数据?@Dormentroat:listbox将使用ToString()默认情况下,如果没有指定其他绑定,它将首先将每个条目强制转换为对象。ToString()在对象上是虚拟的,因此可以在类中正确找到。