XAML表格数据视图

本文关键字:视图 数据 表格 XAML | 更新日期: 2023-09-27 18:03:42

我正在使用c#和XAML开发Windows Store应用程序。我在下面的代码中显示一个名为greetingOutput的文本块中的数据。

try
{
    var response = navigationParameter.ToString();
    var serializer = new DataContractJsonSerializer(typeof(QualityRecordsRootObject));
    var stream = new MemoryStream(Encoding.UTF8.GetBytes(response));
    QualityRecordsRootObject qualityRecordsRootObject = (QualityRecordsRootObject)serializer.ReadObject(stream);
    greetingOutput.Text = String.Format("{0,60}{1,60}{2,60}{3,60}",
                          "Brand",
                          "Printer",
                          "Printer Location",
                          "Date Received");
    greetingOutput.Text += "'n'n";


    for (int i = 0; i < qualityRecordsRootObject.auditDTOList.Count(); i++)
    {
        greetingOutput.Text += String.Format("{0,60}{1,60}{2,60}{3,60}",
                         qualityRecordsRootObject.auditDTOList[i].brandName,
                         qualityRecordsRootObject.auditDTOList[i].printerName,
                         qualityRecordsRootObject.auditDTOList[i].printerLocationName,
                         qualityRecordsRootObject.auditDTOList[i].receivedDate);
        greetingOutput.Text += "'n";
    }
}
catch (Exception ex)
{
    Debug.WriteLine("exception: " + ex.Message);
    greetingOutput.Text += "    No Records Found!";
}

但是看起来不太好;我想要一个表格数据视图,看起来不错。在XAML中有什么解决方法吗?此外,我想添加功能到每一行,这样,如果我单击一行,它去到一个特定的链接。

XAML表格数据视图

我将使用DataGrid。这是DataGrid的msdn: http://msdn.microsoft.com/en-ca/library/system.windows.forms.datagrid.aspx

我像下面这样使用Listbox来给出所需的视图。

<ListBox Name="ResultListBox" 
         Height="500" Width="1000" Margin="0,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"
         Visibility="Visible" SelectionChanged="ResultListBoxSelectionChanged" 
          >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                <TextBlock Width="250" Text="{Binding brandName}"   />
                                <TextBlock Width="250" Text="{Binding printerName}"   />
                                <TextBlock Width="250" Text="{Binding printerLocationName}"  />
                                <TextBlock Width="250" Text="{Binding receivedDate}"  />
                </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

, c#后面的代码如下:

ResultListBox.ItemsSource = qualityRecordsRootObject.auditDTOList;

我希望这能帮助到大家。