Ramesh Ramesh - 1 month ago 27
C# Question

XAML tabular data view

I am developing Windows Store app using C# and XAML. I am displaying data in a text block called

greetingOutput
in the following code.

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!";

}


But it is not looking good; I want to have tabular data view which looks good. Is there any workaround in XAML? Also I would like to add functionality to each row so that if I click a row, It goes to a particular link.

Answer

I used Listbox as follows to give the required view.

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

and for code behind C# as follows.

ResultListBox.ItemsSource = qualityRecordsRootObject.auditDTOList;

I hope this would help someone.