Link to home
Start Free TrialLog in
Avatar of OAC Technology
OAC TechnologyFlag for United States of America

asked on

Issue defining datatype using UWP.

We're trying to build a datagrid connected to an SQL database in UWP as a proof of concept. We're running into an issue declaring the DataTemplate that it doesn't understand the DataType, and I can't get it to process it properly. This has to be defined as we're binding the textboxes to the SQL database. We were using this site for reference https://docs.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases    (we've made changes to suite our needs)

Code segment throwing errors
<DataTemplate x:DataType="local:Product">

The.xaml page with the errors (line 48)
<Page
    x:Class="App3.Views.TicketsPage"
    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"
    Style="{StaticResource PageStyle}"
    mc:Ignorable="d">

    <Grid x:Name="ContentArea">
        <Pivot
            x:Name="Items">
            <PivotItem Header="Open">
                <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
                    <RelativePanel>
                        <ListView Name="WorklogList"
                            SelectionMode="Single"
                            ScrollViewer.VerticalScrollBarVisibility="Auto"
                            ScrollViewer.IsVerticalRailEnabled="True"
                            ScrollViewer.VerticalScrollMode="Enabled"
                            ScrollViewer.HorizontalScrollMode="Enabled"
                            ScrollViewer.HorizontalScrollBarVisibility="Auto"
                            ScrollViewer.IsHorizontalRailEnabled="True"
                            Margin="20">
                            <ListView.HeaderTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal"  >
                                        <TextBlock Text="TaskID" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Status" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Company Name" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Customer Notes" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Assigned To" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Time" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Modified" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Created" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Invoice#" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Paid" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Internal Notes" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Miles" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Requires Follow Up" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Priority" Margin="8,0" Width="50"/>
                                        <TextBlock Text="Quoted Time" Margin="8,0" Width="50"/>
                                        <TextBlock Text="QA" Margin="8,0" Width="50"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.HeaderTemplate>
                            <ListView.ItemTemplate>
                                <DataTemplate x:DataType="local:Product">
                                    <StackPanel Orientation="Horizontal" >
                                        <TextBlock Name="TaskID"
                                            Text="{x:Bind TaskID}"
                                            Width="50" />
                                        <TextBlock Name="Status"
                                            Text="{x:Bind Status}"
                                            Width="50" />
                                        <TextBlock Name="Company Name"
                                            Text="{x:Bind CompanyName}"
                                            Width="50" />
                                        <TextBlock Name="Customer Notes"
                                            Text="{x:Bind CustomerNotes}"
                                            Width="50" />
                                        <TextBlock Name="Assigned To"
                                            Text="{x:Bind AssignedTo}"
                                            Width="50" />
                                        <TextBlock Name="Time"
                                            Text="{x:Bind Time}"
                                            Width="50" />
                                        <TextBlock Name="Modified"
                                            Text="{x:Bind Modified}"
                                            Width="50" />
                                        <TextBlock Name="Created"
                                            Text="{x:Bind Created}"
                                            Width="50" />
                                        <TextBlock Name="Invoice#"
                                            Text="{x:Bind Invoice}"
                                            Width="50" />
                                        <TextBlock Name="Paid"
                                            Text="{x:Bind Paid}"
                                            Width="50" />
                                        <TextBlock Name="Internal Notes"
                                            Text="{x:Bind InternalNotes}"
                                            Width="50" />
                                        <TextBlock Name="Miles"
                                            Text="{x:Bind Miles}"
                                            Width="50" />
                                        <TextBlock Name="Requires Follow Up"
                                            Text="{x:Bind RequiresFollowUp}"
                                            Width="50" />
                                        <TextBlock Name="Priority"
                                            Text="{x:Bind Priority}"
                                            Width="50" />
                                        <TextBlock Name="Quoted Time"
                                            Text="{x:Bind QuotedTime}"
                                            Width="50" />
                                        <TextBlock Name="QA"
                                            Text="{x:Bind QA}"
                                            Width="50" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </RelativePanel>
                </Grid>
            </PivotItem>
            <PivotItem Header="All">
                <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
                </Grid>
            </PivotItem>
            <PivotItem Header="Today">
                <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
                </Grid>
            </PivotItem>
            <PivotItem Header="Unbilled">
                <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
                </Grid>
            </PivotItem>
            <PivotItem Header="Parts">
                <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
                </Grid>
            </PivotItem>
            <PivotItem Header="FollowUp">
                <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
                </Grid>
            </PivotItem>
        </Pivot>
    </Grid>
</Page>

Open in new window


The underlying cs:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Runtime.CompilerServices;

using Windows.UI.Xaml.Controls;

namespace App3.Views
{
    public sealed partial class TicketsPage : Page, INotifyPropertyChanged
    {
        public TicketsPage()
        {
            InitializeComponent();
            WorklogList.ItemsSource = GetProducts((App.Current as App).ConnectionString);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public class Worklog : INotifyPropertyChanged
        {
            public int TaskID { get; set; }
            // public string Status { get { return ProductID.ToString(); } }
            public string Status { get; set; }
            public string CompanyName { get; set; }
            public string CustomerNotes { get; set; }
            public string AssignedTo { get; set; }
            public decimal Time { get; set; }
            public DateTime Modified { get; set; } // DATE/TIME
            public DateTime Created { get; set; } // DATE/TIME
            public int Invoice { get; set; }
            public int Paid { get; set; } // Number?
            public string InternalNotes { get; set; }
            public int Miles { get; set; }
            public bool RequiresFollowUp { get; set; }
            public string Priority { get; set; }
            public string QuotedTime { get; set; }
            public int QA { get; set; }

            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }

        }

        public ObservableCollection<Worklog> GetProducts(string connectionString)
        {
            const string GetWorklogQuery = "select TaskID, Status, CompanyName, CustomerNotes, AssignedTo, Time, Modified, Created, Invoice, Paid, InternalNotes," +
               " Miles, RequiresFollowUp, Priority, QuotedTime, QA";

            var products = new ObservableCollection<Worklog>();
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    if (conn.State == System.Data.ConnectionState.Open)
                    {
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = GetWorklogQuery;
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    var worklog = new Worklog();
                                    worklog.TaskID = reader.GetInt32(0);
                                    worklog.Status = reader.GetString(1);
                                    worklog.CompanyName = reader.GetString(2);
                                    worklog.CustomerNotes = reader.GetString(3);
                                    worklog.AssignedTo = reader.GetString(4);
                                    worklog.Time = reader.GetInt32(5);
                                    worklog.Modified = reader.GetDateTime(6);
                                    worklog.Created = reader.GetDateTime(7);
                                    worklog.Invoice = reader.GetInt32(8);
                                    worklog.Paid = reader.GetInt32(9);
                                    worklog.InternalNotes = reader.GetString(10);
                                    worklog.Miles = reader.GetInt32(11);
                                    worklog.RequiresFollowUp = reader.GetBoolean(12);
                                    worklog.Priority = reader.GetString(13);
                                    worklog.QuotedTime = reader.GetString(14);
                                    worklog.QA = reader.GetInt32(15);
                                    products.Add(worklog);
                                }
                            }
                        }
                    }
                }
                return products;
            }
            catch (Exception eSql)
            {
                Debug.WriteLine("Exception: " + eSql.Message);
            }
            return null;
        }

Open in new window

Avatar of Kelvin McDaniel
Kelvin McDaniel
Flag of United States of America image

The problem appears to be that there is no definition of the Product class in the App3.Views namespace.

Taken from this explanation:
local: and common:

"local:" is a prefix that is often mapped for you within the XAML pages for a templated UWP app project. It's mapped to refer to the same namespace that's created to contain the x:Class attribute and code for all the XAML files including app.xaml. So long as you define any custom classes you want to use in XAML in this same namespace, you can use the local: prefix to refer to your custom types in XAML.

Possible choices:
1. Try moving the Product class into the App3.Views namespace.

2. Create a derivative class in the App3.Views namespace and using that new custom class instead.
ASKER CERTIFIED SOLUTION
Avatar of OAC Technology
OAC Technology
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial