2011-07-03 61 views
0

民間,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' (Name ='');目標屬性是'文字' (類型'System.String')..

我會做什麼錯?

+0

列表框顯示FirstName + LastName而不是您在CustomerTemplate中聲明的內容 – anivas

回答

0

通過將其設置爲ObservableCollection<IBaseModel>,您可以將所有子對象有效地轉換爲IBaseModel,該對象沒有成員。

在這種情況下使它成爲ObservableCollection<EmployeeModel>

+0

但是,ListBox控件如何在ObservableCollection 中不顯示數據的轉換? – dormantroot

+0

@dormantroot:默認情況下,Listbox將使用ToString(),因爲如果沒有指定其他綁定,它將首先將每個條目轉換爲Object。 ToString()在Object上是虛擬的,因此可以在你的類中正確找到。 –