Silverlight Datagrid的有趣问题

时间:2011-07-03 20:21:49

标签: silverlight datagrid

民间,

我遇到了Silverlight DataGrid数据绑定的一个有趣问题。它可能是b / c我没有正确绑定数据源。这是对象&可观察的集合

/// <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>

列表框显示所有员工,但DataGrid不显示。我甚至没有看到DataGrid。我在输出窗口中看到此错误消息:

  

'System.Collections.ObjectModel.ObservableCollection 1[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'   (名称= '');目标属性是'文字'   (输入'System.String')..

我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

通过使它成为ObservableCollection<IBaseModel>,您实际上将所有子对象都转换为IBaseModel,它没有成员。

在这种情况下,请将其设为ObservableCollection<EmployeeModel>

相关问题