【问题标题】:Interesting Issue with Silverlight DatagridSilverlight Datagrid 的有趣问题
【发布时间】:2011-09-27 18:20:27
【问题描述】:

伙计们,

我遇到了一个关于 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>

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

'System.Collections.ObjectModel.ObservableCollection1[AdventureWorksManagement.Model.IBaseModel]' 'System.Collections.ObjectModel.ObservableCollection1[AdventureWorksManagement.Model.IBaseModel]' (哈希码=54025633)。 BindingExpression:路径='FirstName' DataItem='System.Collections.ObjectModel.ObservableCollection`1[AdventureWorksManagement.Model.IBaseModel]' (哈希码=54025633);目标元素是 'System.Windows.Controls.TextBlock' (名称='');目标属性是“文本” (类型'System.String')..

我做错了什么?

【问题讨论】:

  • 列表框是否显示 FirstName + LastName 而不是您在 CustomerTemplate 中声明的内容

标签: silverlight datagrid


【解决方案1】:

通过将其设为ObservableCollection&lt;IBaseModel&gt;,您实际上将所有子对象转换为没有成员的 IBaseModel。

在这种情况下,将其设为ObservableCollection&lt;EmployeeModel&gt;

【讨论】:

  • 但是 ListBox 控件如何在 ObservableCollection 中显示数据而不进行转换?
  • @dormantroot:默认情况下,列表框将使用 ToString(),因为如果没有指定其他绑定,它将首先将每个条目转换为对象。 ToString() 在 Object 上是虚拟的,因此可以在您的类中正确找到。
猜你喜欢
  • 2011-09-21
  • 2011-06-17
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多