how can i add load more for my list box when we scrolling list box in windows phone 8.1 apps - xaml

how can i display 5 items when we scrolling i want load more.
<ListBox.ItemTemplate>
<DataTemplate >
<Border BorderThickness="0,0,0,1" BorderBrush="Black"
HorizontalAlignment="Stretch" Width="400"
Margin="-8,0,-8,0">
<Grid Height="130"
RenderTransformOrigin="0.37,0.52">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid HorizontalAlignment="Left"
Height="104"
Grid.Row="0"
VerticalAlignment="Top"
Width="115" Background="White"
Margin="05,-4,0,0">
<Image Source="{Binding company_logo}" Stretch="None"
Margin="10,0,0,0"/>
</Grid>
<Grid Height="104" HorizontalAlignment="Right"
Grid.Row="0"
VerticalAlignment="Top" Width="280"
Background="White" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="307*"/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left"
Margin="0,0,0,0"
TextWrapping="Wrap"
Text="{Binding product_name}"
VerticalAlignment="Top"
FontSize="20"
Foreground="Orange"
Grid.Column="1"/>
<TextBlock Text="Reviews:"
Margin="97.5,27,125,51"
Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left"
Margin="145,30,0,0"
TextWrapping="Wrap"
Text="{Binding product_reviews}"
VerticalAlignment="Top"
FontSize="10" Width="50"
Foreground="Orange"
Grid.Column="1"/>
<TextBlock Text="Rating:" FontSize="15"
Margin="0,25,232,51"
Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" Margin="50,30,0,0"
TextWrapping="Wrap" Text="{Binding product_rating}"
VerticalAlignment="Top" FontSize="10"
Foreground="Black" Grid.Column="1"/>
<Image Source="Assets/clock-icon.png" Margin="165,25,0,0" Width="25" Height="25"/>
<TextBlock Text="Created Date" FontSize="10"
Margin="155,29,70,62" Foreground="Red"
Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" Width="59"
Margin="218,29,0,0"
TextWrapping="Wrap"
Text="{Binding created_date}"
VerticalAlignment="Top" FontSize="10"
Foreground="Black"
Grid.Column="1" Height="13">
</TextBlock>
<TextBlock Text="Updated Date:" FontSize="10"
Margin="155,39,70,52" Foreground="Red"
Grid.Column="1"/>
<TextBlock HorizontalAlignment="Left" Width="59"
Margin="218,39,0,0"
TextWrapping="Wrap"
Text="{Binding updated_date}"
VerticalAlignment="Top" FontSize="10"
Foreground="Black"
Grid.Column="1" Height="13">
</TextBlock>
<TextBlock HorizontalAlignment="Left" Margin="3,58,0,0" TextWrapping="Wrap"
Text="{Binding product_description}"
VerticalAlignment="Top" FontSize="10"
Foreground="Black" Height="40"
Width="293" Grid.Column="1" />
</Grid>
<TextBlock HorizontalAlignment="Left" Margin="15,100,0,0"
TextWrapping="Wrap" Width="250"
Text="{Binding companyName}"
VerticalAlignment="Top" FontSize="15"
Foreground="Black"/>
<Button Content="Contact" Foreground="Red"
FontSize="10"
Margin="120,88,-29,440.667"
Height="25" Grid.Column="1"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and my code is below
List<products> products1 = new List<products>();
try
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("something");
var url = "data/getProductsData";
Parameters product = new Parameters();
product.user_id = "1";
product.platform = "Android";
string json = JsonConvert.SerializeObject(product);
StringContent queryString = new StringContent(json);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsync(url, queryString);
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync();
Response my = JsonConvert.DeserializeObject<ResponseWrapper>(data.Result.ToString()).response;
LoadingBar.IsEnabled = true;
LoadingBar.Visibility = Visibility.Visible;
var myData =data.Result.ToString();
//await new MessageDiaglog("Data Loaded!").ShowAsync();
LoadingBar.IsEnabled = false;
LoadingBar.Visibility = Visibility.Collapsed;
foreach (var item in my.content.products)
{
products1.Add(item);
}
}
lstbox.ItemsSource = products1;
}
}
catch (Exception ex)
{
MessageDialog message = new MessageDialog(ex.Message);
message.ShowAsync();

first of all :
1- change this line
List<products> products1 = new List<products>();
with this
public static ObservableCollection<products> products1 = new ObservableCollection<products>();
2- get scrolling postion of your listbox
public static ScrollViewer GetScrollViewer(DependencyObject dpj)
{
if (dpj is ScrollViewer) return dpj as ScrollViewer;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dpj); i++)
{
var child = VisualTreeHelper.GetChild(dpj, i);
var result = GetScrollViewer(child);
if (result != null)
{
return result;
}
}
return null;
}
3- create a handler of listbox_loaded.
private void lstsource_Loaded(object sender, RoutedEventArgs e)
{
ScrollViewer viewer = GetScrollViewer(this.lstsource);
viewer.ViewChanged += lstSoucrce_ViewChanged;
}
4- on view changed ...
private async void lstSoucrce_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
var sv = (ScrollViewer)VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(this.lstsource, 0), 0);
var verticaloffset = sv.VerticalOffset;
var maxVerticleOffset = sv.ScrollableHeight;
if (maxVerticleOffset < 0 || verticaloffset == maxVerticleOffset)
{
//this will fired up when you will reach end of list
//load your more data here
}

Related

Hide and Show Grid in ListView on Image inside Listview in Tapped Event Function

On the click of image inside a List view ,I want Like to hide a Grid which is in ListView.GroupHeaderTemplate. I have made Bindings also- IsVisible="{Binding ShowGrid}" on that Grid. I have taken Reference from https://stackoverflow.com/a/55274297/10223206 this post .But Struck , Visibility is not Changing . I am Sharing my Code for better understanding
View Model-
private bool _ShowGrid = false;
public bool ShowGrid
{
get => _ShowGrid;
set
{
_ShowGrid = value;
RaisePropertyChanged();
}
}
In cs:_(Image Tap-Image Name - downarrow.png)
private void TapGestureRecognizer_Tapped_1(object sender, EventArgs e)
{
try
{
Image image = sender as Image;
string source = image.Source as FileImageSource; //Getting the name of source as string
if (String.Equals(source, "downarrow.png"))
{
image.Source = "uparrow.png";
viewModel.ShowGrid = false;
}
else
{
image.Source = "downarrow.png";
viewModel.ShowGrid = true;
}
}
catch(Exception ex)
{
var m = ex.Message;
}
}
Xaml-
<ListView x:Name="MyListView" IsGroupingEnabled="true" Footer=" "
HasUnevenRows="True" ItemSelected="MyListView_ItemSelected"
SeparatorColor="Transparent" VerticalOptions="FillAndExpand"
SeparatorVisibility="None" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid IsVisible="{Binding ShowGrid}" >
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Image Grid.Column="0" Aspect="AspectFit" HeightRequest="20" WidthRequest="30"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Source="checked.png" />
<Label Grid.Column="1" Text="{Binding SatusName}" FontSize="10"
HorizontalOptions="StartAndExpand"
VerticalOptions="CenterAndExpand" />
<Label Grid.Column="2" Text="{Binding Date}" FontSize="10"
HorizontalOptions="StartAndExpand"
VerticalOptions="CenterAndExpand" />
</Grid>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Grid RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" VerticalOptions="CenterAndExpand"
Text="{Binding personName}" TextColor="{StaticResource PrimaryBlue}"
FontSize="Medium" FontAttributes="Bold"/>
<Label Grid.Row="0" Grid.Column="1"
Text="{Binding Amount}" VerticalOptions="CenterAndExpand"
TextColor="{StaticResource PrimaryBlue}"
FontSize="Medium" />
<Image Source="downarrow.png" Grid.Row="0" Grid.Column="2" x:Name="ArrowImage"
HeightRequest="20" WidthRequest="30"
HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand" >
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"/>
</Image.GestureRecognizers>
</Image>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
But Nothing Seems Work , it not hiding Grid on IMAGE CLICK. Kindly help me find a Solution.
.cs
grouped = new ObservableCollection<GroupedOrderModel>();
var person1Group = new GroupedOrderModel() { personName = "Personal loan" ,Amount="100"};
var person2Group = new GroupedOrderModel() { personName = "Car Loan",Amount="300" };
var person3Group = new GroupedOrderModel() { personName = "Rent Loan",Amount="400" };
person1Group.Add(new StaffLoanStatus() { SatusName = "Approved", Date = "23-01-2019" });
person1Group.Add(new StaffLoanStatus() { SatusName = "Pending", Date = "20-01-2019" });
person1Group.Add(new StaffLoanStatus() { SatusName = "Declined", Date = "19-01-2019" });
person2Group.Add(new StaffLoanStatus() { SatusName = "Approved", Date = "23-01-2019" });
person2Group.Add(new StaffLoanStatus() { SatusName = "Pending", Date = "20-01-2019" });
grouped.Add(person1Group);
grouped.Add(person2Group);
//Person3 without OrderModel
grouped.Add(person3Group);
MyListView.ItemsSource = grouped;
The reason is not working is the BindingContext since ListViews ItemsSource is it binding context anything outside it is alien to that DataTemplate try doing this:
IsVisible="{Binding Path=BindingContext.ShowGrid, Source={x:Reference MyListView}}"

Customized XAML Spinner

I am trying to make a Spinner for a Windows Phone 8.1 App. I want the Spinner to have 2 wheels: A list of numbers on one, and a list of words (not AM/PM) on the other. Something similar to the TimePicker:
I did not see any options that worked this way. (ComboBox is the closest I found, but it does not spin.)
Is there a way to customize the TimePicker? Or create a Spinner like it?
If you look through sources of DatePicker, you'll see them use Microsoft.Phone.Controls.Primitives.LoopingSelector as wheel. It is public class and you can use it from Windows Phone Toolkit.
You also can see example of usage in DatePicker sources.
here's a starting point using a ScrollViewer with VerticalSnapPointsType="Mandatory"
When ScrollViewer.ViewChanged use ScrollViewer.VerticalOffset and the height of your container to calculate the selected item. Sorry Stackpanel does not support itemssource binding so you may have to add the elements in the code behind.
the XAML
<ScrollViewer Width="70"
Loaded="ScrollViewer_OnLoaded"
VerticalContentAlignment="Center"
VerticalSnapPointsType="Mandatory"
VerticalSnapPointsAlignment="Center">
<StackPanel Margin="0,200" x:Name="StackPanel">
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="1"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="2"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="3"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="4"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="5"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="6"></TextBlock>
</Border>
</Grid>
</StackPanel>
</ScrollViewer>
The code behind
private void ScrollViewer_OnLoaded(object sender, RoutedEventArgs e)
{
ScrollViewer sv = sender as ScrollViewer;
sv.ChangeView(0, 200, null, true);
sv.ViewChanged += sv_ViewChanged;
}
void sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (e.IsIntermediate)
{
return;
}
else
{
ScrollViewer sv = sender as ScrollViewer;
double offset = sv.VerticalOffset;
double stackpanelMargin = 200;
double itemHeight = 80;
int selectedIndex = (int)Math.Round((offset + stackpanelMargin) / itemHeight);
//int selectedIndex = int.Parse(indexOfSelectedItem.ToString());
//get selected item
var StackpanelChildren = StackPanel.Children;
int i = 0;
foreach (var stackpanelChild in StackpanelChildren)
{
if (i == selectedIndex)
{
stackpanelChild.Opacity = 1;
}
else
{
stackpanelChild.Opacity = .5;
}
i++;
}
}
}

window phone - How to disable current select item?

I have a xaml :
<phone:LongListSelector Name="llsSourceNews" ItemsSource="{Binding SourceNews}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid x:Name="gridNews">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{Binding icon}" Stretch="Fill" Height="35" Width="70"></Image>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" Foreground="White" TextWrapping="Wrap" FontSize="24" VerticalAlignment="Center"></TextBlock>
<Image Grid.Row="0" Grid.Column="2" Source="/Images/Add-New.png" x:Name="imgAdd" Tap="imgAdd_Tap"></Image>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
and tap event :
private void imgAdd_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if(true)
{
this.Visibility = Visibility.Collapsed;
}
}
My problem is : when user tap image,all image is disable.I want to image is disable which is selected.
this in your case refers to the page. If you want to retrieve the image, you have to cast the sender parameter:
private void imgAdd_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if(true)
{
var element = (FrameworkElement)sender;
element.Visibility = Visibility.Collapsed;
}
}

Equal Sized Columns in ListView in Windows Store app

I want to make all the columns in a listview of equal width in the sense, all the columns under username header should be equal width. Currently it is looking like the attached Following is my code,
<ListView HorizontalAlignment="Left" Width="1000" x:Name="MainLIst">
<ListView.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border Background="SaddleBrown" MaxWidth="213">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding FirstName}" VerticalAlignment="Top" Margin="0,0,30,0"/>
</Border>
<Border Background="Maroon" Grid.Column="1" MaxWidth="200">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding LastName}" VerticalAlignment="Top" Margin="0,0,30,0"/>
</Border>
<Border Background="Brown" Grid.Column="2" >
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Username}" VerticalAlignment="Top" Margin="0,0,30,0"/>
</Border>
<Border Background="RosyBrown" Grid.Column="3" >
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Passwrd}" VerticalAlignment="Top" Grid.Column="3" Margin="0,0,30,0"/>
</Border>
</Grid>
</DataTemplate>
<DataTemplate x:Key="HeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border Background="SaddleBrown" MaxWidth="213">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="First Name" VerticalAlignment="Top" Margin="0,0,30,0"/>
</Border>
<Border Background="Maroon" Grid.Column="1" MaxWidth="200">
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Last Name" VerticalAlignment="Top" Margin="0,0,30,0"/>
</Border>
<Border Background="Brown" Grid.Column="2" >
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="User Name" VerticalAlignment="Top" Margin="0,0,30,0"/>
</Border>
<Border Background="RosyBrown" Grid.Column="3" >
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top" Grid.Column="3" Margin="0,0,30,0"/>
</Border>
</Grid>
</DataTemplate>
</ListView.Resources>
<ListView.HeaderTemplate>
<StaticResource ResourceKey="HeaderTemplate"/>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
<StaticResource ResourceKey="DataTemplate1"/>
</ListView.ItemTemplate>
</ListView>
and Code behind,
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
ObservableCollection<ClassItem> classItemObj = new ObservableCollection<ClassItem>();
classItemObj.Add(new ClassItem() { FirstName = "1234567890", LastName = "1234567890", Passwrd = "sdf", Username = "123445567788894564512345678906" });
classItemObj.Add(new ClassItem() { FirstName = "12345678901234567890", LastName = "1234567890", Passwrd = "sdf", Username = "12344556778889456456" });
classItemObj.Add(new ClassItem() { FirstName = "1234567890", LastName = "123456789012345678901234567890", Passwrd = "sdf", Username = "123445567788894564561234567890" });
classItemObj.Add(new ClassItem() { FirstName = "1234567890", LastName = "12345678901234567890", Passwrd = "sdf", Username = "1234455677888945645612345678901234567890" });
classItemObj.Add(new ClassItem() { FirstName = "1234567890123456789012345678901234567890", LastName = "1234567890", Passwrd = "sdf", Username = "12344556778889456456" });
MainLIst.ItemsSource = classItemObj;
}
}
public class ClassItem
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public string Passwrd { get; set; }
}
}
You currently set the column Widths to Auto. If you specify pixel values - they should be the same.

MVVM binding error?

In the first block below I intentionally miss named one of my binding statements so I could compare to my second block. The difference is in the property not found on 'ag2.item...' line.
item is my model.
In block 2 you can see that it is pointing to my view model (ag2.viewModel.itemViewModel).
What do I need to do in my XAML or my codebehind to get it to point to my class rather than the viewmodel?
Block 1:
BindingExpression path error: 'itemModel1' property not found on
'ag2.item, ag2, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. BindingExpression: Path='itemModel1'
DataItem='ag2.item, ag2, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null'; target element is
'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is
'Text' (type 'String')
Block 2:
BindingExpression path error:'itemModel' property not found on
'ag2.viewModel.itemViewModel, ag2, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. BindingExpression: Path='itemModel'
DataItem='ag2.viewModel.itemViewModel, ag2, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null'; target element is
'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is
'Source' (type 'String')
Code behind to Block 2:
itemViewModel VM = new itemViewModel((Int32)navigationParameter);
DataContext = VM;
I should also note that in block 1 I am doing my binding to a GridView where the ItemSource="{Binding item}" is set.
In block 2 I built my UI using grids and textblocks using this: Text="{Binding Path=itemModel}"
Update: In an effort to try to gain better understanding. I'm putting my code out there: Here is the XAML, below that will be the ViewModel and below that is my Model... I'm new at MVVM so I really don't know what I'm doing incorrectly. Any help is greatly appreciated.
XAML:
<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="autoGarage2.VehicleItemDetailPage"
IsTabStop="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="using:autoGarage2.Common"
xmlns:local="using:autoGarage2"
xmlns:data="using:autoGarage2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<!--
This grid acts as a root panel for the page that defines two rows:
* Row 0 contains the back button and page title
* Row 1 contains the rest of the page layout
-->
<Grid Style="{StaticResource LayoutRootStyle}">
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<!-- Back button and page title -->
<Grid
Style="{StaticResource LayoutRootStyle}" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
<TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1"/>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Width="auto" Margin="50,0,0,0" VerticalAlignment="Top" >
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Border Background="White" BorderBrush="CornflowerBlue" BorderThickness="1">
<Image Source="{Binding Image}" Margin="50"/>
</Border>
<StackPanel VerticalAlignment="Bottom" Background="CornflowerBlue">
<StackPanel Orientation="Horizontal" DataContext="{Binding vehicles}">
<TextBlock Text="{Binding VehicleMake}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource PageSubheaderTextStyle}" Margin="5"/>
<TextBlock Text="{Binding VehicleModel}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource PageSubheaderTextStyle}" Margin="5"/>
</StackPanel>
</StackPanel>
</Grid>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1">
<Grid Margin="20,0,0,20">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Vehicle Make:" Grid.Row="0" Grid.Column="0" FontSize="25" Foreground="Black" />
<TextBox Text="{Binding Path=VehicleMake}" Grid.Row="0" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="Vehicle Model:" FontSize="25" Foreground="Black" Grid.Row="1" Grid.Column="0"/>
<TextBox Text="{Binding VehicleModel}" Grid.Row="1" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="Vehicle Year:" FontSize="25" Foreground="Black" Grid.Row="2" Grid.Column="0"/>
<TextBox Text="{Binding VehicleYear}" Grid.Row="2" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="License Plate:" FontSize="25" Foreground="Black" Grid.Row="3" Grid.Column="0"/>
<TextBox Text="" Grid.Row="3" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="VIN #" FontSize="25" Foreground="Black" Grid.Row="4" Grid.Column="0"/>
<TextBox Text="" Grid.Row="4" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1" />
<TextBlock Text=" Current Mi/Km" FontSize="25" Foreground="Black" Grid.Row="5" Grid.Column="0"/>
<TextBox Text="" Grid.Row="5" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="" FontSize="25" Foreground="Black" Grid.Row="6" Grid.ColumnSpan="2"/>
<TextBlock Text="Last Oil Change" FontSize="25" Foreground="Black" Grid.Row="7" Grid.Column="0"/>
<TextBox Text="" Grid.Row="7" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="Last Oil Change Mi/Km" FontSize="25" Foreground="Black" Grid.Row="8" Grid.Column="0"/>
<TextBox Text="" Grid.Row="8" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="" FontSize="25" Foreground="Black" Grid.Row="9" Grid.ColumnSpan="2"/>
<TextBlock Text="Reminder Mi/Km" FontSize="25" Foreground="Black" Grid.Row="10" Grid.Column="0"/>
<TextBox Text="" Grid.Row="10" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
<TextBlock Text="Reminder Month(s)" FontSize="25" Foreground="Black" Grid.Row="11" Grid.Column="0"/>
<TextBox Text="" Grid.Row="11" Grid.Column="1" FontSize="25" BorderBrush="CornflowerBlue" BorderThickness="1"/>
</Grid>
</StackPanel>
</Grid>
</Grid>
</common:LayoutAwarePage>
View Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Windows.Foundation.Collections;
using System.IO;
namespace autoGarage2.viewModel
{
class vehicleViewModel
{
private IList<vehicle> m_vehicles;
private IList<vehicle> m_vehicleItem;
public IList<vehicle> vehicles
{
get { return m_vehicles; }
set { m_vehicles = value; }
}
public IList<vehicle> vehicleItem
{
get { return m_vehicleItem; }
set { m_vehicleItem = value; }
}
private IList<vehicle> getVehicleDetail(Int32 vId)
{
var vehicleItem =
from v in vehicles
where v.VehicleId == vId
select v;
if (vId > 0)
{
//vehicles.Clear();
m_vehicles = new List<vehicle>();
foreach (var item in vehicleItem)
{
m_vehicles = new List<vehicle>
{
new vehicle(item.VehicleId, item.VehicleMake.ToString(), item.VehicleModel.ToString(), item.VehicleYear, item.Image.ToString())
};
//vehicle myVehicle = new vehicle(item.VehicleId, item.VehicleMake.ToString(), item.VehicleModel.ToString(), item.VehicleYear, item.Image.ToString());
//m_vehicles.Add(myVehicle);
}
}
return m_vehicles;
}
public vehicleViewModel(Int32 vId)
{
m_vehicles = new List<vehicle>
{
new vehicle(1, "Mazda", "3", 2011, "Assets/car2.png"),
new vehicle(2, "Chevy", "Tahoe", 2004, "Assets/jeep1.png"),
new vehicle(3, "Honda", "Goldwing", 2007 ,"Assets/moto1.png")
};
if (vId > 0)
{
//m_vehicles = new List<vehicle>();
//m_vehicles =
//getVehicleDetail(vId);
m_vehicles = new List<vehicle>
{
new vehicle(2, "Chevy", "Tahoe", 2004, "Assets/jeep1.png"),
};
}
}
#region dbCode
//string dbName = "vehicle.db";
//var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);
//using (var db = new SQLite.SQLiteConnection(dbPath))
// {
// var list = db.Table<vehicle>().ToList();
// m_vehicles = new List<vehicle>();
// for (Int32 i = 0; i < list.Count; i++)
// {
// //m_vehicles.Add(db.Table<vehicle>().ToList());
// }
// }
//foreach (vehicle item in m_vehicles)
//{
// AllItems.Add(item);
//}
#endregion
}
}
Model:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using SQLite;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace autoGarage2
{
class vehicle : autoGarage2.Common.BindableBase
{
public vehicle()
{
}
public vehicle(string imagePath)
{
this._imagePath = imagePath;
}
public vehicle(Int32 vId, string vMake, string vModel, Int16 vYear, string imagePath)
{
this.m_vehicleID = vId;
this.m_vehicleMake = vMake;
this.m_vehicleModel = vModel;
this.m_vehicleYear = vYear;
this.m_vehicleName = vMake + " " + vModel;
this._imagePath = imagePath;
}
private Int32 m_vehicleID;
private String m_vehicleMake;
private String m_vehicleModel;
private Int16 m_vehicleYear;
private string m_vehicleName;
private ImageSource _image = null;
private String _imagePath = null;
private static Uri _baseUri = new Uri("ms-appx:///");
//[AutoIncrement, PrimaryKey]
public Int32 VehicleId
{
get
{
return m_vehicleID;
}
set
{
m_vehicleID = value;
OnPropertyChanged("VehicleId");
}
}
public String VehicleMake
{
get
{
return m_vehicleMake;
}
set
{
m_vehicleMake = value;
OnPropertyChanged("VehicleMake");
}
}
public String VehicleModel
{
get
{
return m_vehicleModel;
}
set
{
m_vehicleModel = value;
OnPropertyChanged("VehicleModel");
}
}
public Int16 VehicleYear
{
get
{
return m_vehicleYear;
}
set
{
m_vehicleYear = value;
OnPropertyChanged("VehicleYear");
}
}
public string VehicleName
{
get
{
return m_vehicleName;
}
set
{
m_vehicleName = value;
OnPropertyChanged("VehicleName");
}
}
public ImageSource Image
{
get
{
if (this._image == null && this._imagePath != null)
{
this._image = new BitmapImage(new Uri(vehicle._baseUri, this._imagePath));
}
return this._image;
}
set
{
this._imagePath = null;
this.SetProperty(ref this._image, value);
}
}
public void SetImage(String path)
{
this._image = null;
this._imagePath = path;
this.OnPropertyChanged("Image");
}
}
}
It sounds like you are trying to databind your TextBlock to a different object, not to the ViewModel (which is the DataContext of your window/control). If that is correct, you'll need to set the DataContext of the TextBlock or the parent Grid to the object you want to DataBind to.
The DataContext is used in determining the path to DataBind to for a control, and it inherits down the visual tree. So if your DataContext is set to MyViewModel, and you use Text="{Binding Path=itemModel}", your binding path will be MyViewModel.itemModel.
If you don't want to include MyViewModel in the binding path, you'll need to change the DataContext of the control in question, or of a containing control. For MVVM, this is often done by having the other object exposed as a property of the ViewModel. So if your MyViewModel had a property ItemModel:
public class ItemModel
{
public string Property1 { get; }
public string Property2 { get; }
}
public class MyViewModel
{
public ItemModel ItemModel { get; private set; }
}
Then your XAML could look like this (assuming MyViewModel is the DataContext of the parent window/control).
<Grid Grid.Row="1" DataContext="{Binding ItemModel}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding property1}"/>
<TextBlock Text="{Binding Property2}" Grid.Row="1"/>
</Grid>
And the two textboxes would be bound to Property1 and Property2 of the ItemModel object.
You seem to be trying to bind to a list but you are not using any ItemsControl in your XAML. You should probably use something like a ListView, bind its ItemsSource to your vehicles or vehicleItem lists, use an ItemTemplate/DataTemplate to define the look of each item in the collection, use ObservableCollection if your collection changes or raise INotifyPropertyChanged.PropertyChanged notifications if you swap out your collection, etc. Otherwise I would suggest reading something about binding ItemsControls or a general book about XAML, like Adam Nathan's WPF Unleashed. It seems like you are setting your m_vehicles only to replace it with a new one in the next statement. Also - you are setting the DataContext of the StackPanel to your list, which is allowed, but it does not work, since the DataContext of the elements of your StackPanel will still be the entire list and not its items, which is what you get by using an ItemsControl.
I was able to get my data to show up after a weekend of not looking at it. Basically I noticed that I was getting a binding error that stated that it couldn't find the property in my autoGarage2.vehicles list. So for grins I prefaced the binding like this:
{Binding vehicles[0].vehicleModel}
Next time I ran it, the data was there. After thinking about it some more, I decided to create a single object of vehicle and instead of populating the list object I'm populating just the single vehicle property. Now I'm doing something like this:
{Binding vehicleSingle.vehicleModel}
Thanks for every ones help. I guess this is just a nuance of how MVVM works in XAML...