简体   繁体   English

Silverlight:为什么DataGrid显示空行?

[英]Silverlight: Why Is DataGrid displaying empty rows?

In Silverlight 3, I am tring to produce a basic, populated DataGrid, but the DataGrid is displaying 4 empty rows. 在Silverlight 3中,我试图生成一个基本的,填充的DataGrid,但该DataGrid显示4个空行。

The code snippets for the page.xaml and page.xaml.cs are below. 以下是page.xaml和page.xaml.cs的代码段。

I think the data is correctly contained in e.Results, because when I step through the codebehind, the web service is returning the List correctly populated, e.Results shows a count of 4, and the DataGrid displays four empty rows. 我认为数据正确包含在e.Result中,因为当我逐步浏览代码背后时,Web服务将返回正确填充的List,e.Results显示为4,而DataGrid显示四个空行。 (I have not yet figured out how to see what exactly is in e.Results) (我尚未弄清楚如何查看e.Results中的确切内容)

Here is the relevant code in page.xaml.cs: 这是page.xaml.cs中的相关代码:

void Page_Loaded(object sender, RoutedEventArgs e)
{
  Service1Client service = new Service1Client();
  service.GetAccountsCompleted += new
   EventHandler<GetAccountsCompletedEventArgs>(service_GetAccountsCompleted );
  service.GetAccountsAsync();

}

void service_GetAccountsCompleted(object sender, GetAccountsCompletedEventArgs e)
{
  this.grdData.ItemsSource = e.Result;
}

Here is the xaml definition of the DataGrid: 这是DataGrid的xaml定义:

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="BegSilver.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
  <data:DataGrid x:Name="grdData"  Margin="15" AutoGenerateColumns="False">

  <data:DataGrid.Columns>

    <data:DataGridTextColumn Binding="{Binding acpk}" Header="acpk"></data:DataGridTextColumn>
    <data:DataGridTextColumn Binding="{Binding acctnumber}" Header="acctnumber"></data:DataGridTextColumn>
    <data:DataGridTextColumn Binding="{Binding name}" Header="name"></data:DataGridTextColumn>
    <data:DataGridTextColumn Binding="{Binding type}" Header="type"></data:DataGridTextColumn>

  </data:DataGrid.Columns>

</data:DataGrid>
</Grid>
</UserControl>

Any help would be greatly appreciated. 任何帮助将不胜感激。

I had the exact same problem. 我有同样的问题。 It turned out that the Foreground text color and background colors of the cells were the same. 事实证明,单元格的前景文本颜色和背景颜色是相同的。 Just change those and you should be good. 只要改变那些,你就应该很好。 There's some examples here: http://silverlight.net/forums/t/42372.aspx 这里有一些示例: http : //silverlight.net/forums/t/42372.aspx

Add a foreground color style to your datagrid to see the text. 向您的数据网格添加前景色样式以查看文本。 Something like this: 像这样:

<UserControl.Resources>
    <Style x:Key="gridStyle" TargetType="Controls:DataGrid">
        <Setter Property="Foreground" Value="Black" />
    </Style>
</UserControl.Resources>


<Controls:DataGrid x:Name="grid" AutoGenerateColumns="True" Style="{StaticResource gridStyle}">

... ...

You can't perform binding like that directly from e.Result. 您不能直接从e.Result执行这样的绑定。

<data:DataGridTextColumn Binding="{Binding acpk}" Header="acpk">
</data:DataGridTextColumn>

You're on the right track working with e.Result but you really should put the results into a collection then work with that. 使用e.Result的方向正确,但您确实应该将结果放入集合中,然后使用它。 I know it's redundant but it seems that a collection of a collection or a list of a collection etc is how it's done the "Silverlight way" 我知道这是多余的,但似乎是通过“ Silverlight方式”完成的是一个集合的集合或一个集合的列表等。

Try something like this. 尝试这样的事情。 Create a class. 创建一个类。 This will process your results when looping through e.Result. 遍历e.Result时,它将处理您的结果。

public class Recording
{
  public Recording() { 
  }
  public Recording(string artistName, string cdName, DateTime release)
  { Artist = artistName; Name = cdName; ReleaseDate = release; }
  public string Artist { get; set; }
  public string Name { get; set; }
  public DateTime ReleaseDate { get; set; }
  // Override the ToString method.
  public override string ToString() 
  { 
    return Name + " by " + Artist + ", Released: " + ReleaseDate.ToShortDateString();
  }
} 

In the code behind where you have called your service create a collection. 在调用服务的代码后面创建一个集合。 This will collect e.Result content and pass it to the class mentioned. 这将收集e.Result内容并将其传递给提到的类。

 public ObservableCollection<Recording> MyMusic   = new ObservableCollection<Recording>();

In the completed args event handler add something like this. 在完成的args事件处理程序中,添加类似以下内容。

private void  service_GetAccountsCompleted(object sender, GetAccountsCompletedEventArgs e)
{
  foreach (var item in e.Result)
  {   
      MyMusic.Add(new Recording(item.acctnumber, item.acpk, new DateTime(2011, 4, 18)));
      grdData.DataContext = MyMusic;               
  }

I have used this for reference and I think it's worth a look. 我已将此作为参考,我认为值得一看。 It shows how to bind in multiple ways and helped me when learning more about data binding. 它显示了如何以多种方式进行绑定,并且在学习有关数据绑定的更多信息时对我有所帮助。 http://www.silverlight.net/learn/quickstarts/bindingtocontrols/ http://www.silverlight.net/learn/quickstarts/bindingtocontrols/

The column names are case sensitive. 列名区分大小写。 Are you sure it is 你确定是

   Binding="{Binding acpk}"

and not 并不是

   Binding="{Binding Acpk}"

A guess here, but I would venture that rather than setting the itemssource property, you should be setting the DataContext property. 这是一个猜测,但是我要冒险设置而不是设置itemssource属性,而应该设置DataContext属性。 ItemsSource is for collection based controls such as a ListBox etc, and is used to define what collection to use to populate the items. ItemsSource用于基于集合的控件(如ListBox等),并用于定义用于填充项目的集合。

My guess is that you populated an object (Account) from a WCF service 我的猜测是您从WCF服务中填充了一个对象(帐户)
If so, set 如果是这样,请设置

this.grdData.DataContex= e.Result; this.grdData.DataContex = e.Result; in your GetAccountsCompleted handler 在您的GetAccountsCompleted处理程序中

Good Post here sumarrizing the differences between ItemsSource and DataContext http://anyedotnet.blogspot.com/2008/11/datacontext-vs-itemssource.html 这里的好帖子总结了ItemsSource和DataContext之间的区别http://anyedotnet.blogspot.com/2008/11/datacontext-vs-itemssource.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM