wp7 Date Formatting during Binding - c#

I tried following the following example with no luck:
http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter%28v=VS.95%29.aspx
I also tried to follow this as well:
Formatting a date in XAML on WP7
I can't get it to work. When I try to add a reference such as:
<namespace:dateTimeConverter x:Key="MyDateTimeToStringConverter"/>
or
<src:DateConverter x:Key="dateConverter"/>
I'm not sure what I'm doing wrong. Any assistance is appreciated!
Here is my class code:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;
namespace OilChangeApplication
{
public class dateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
return date.ToShortDateString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value as string;
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return DependencyProperty.UnsetValue;
}
}
}
Here is my windows phone form xaml:
<phone:PhoneApplicationPage
x:Class="OilChangeApplication.historyInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.Resources>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="Change your Oil Application 2.0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="history" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<ListBox Grid.Row="1">
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Height="601" HorizontalAlignment="Left" Margin="-3,2,0,0" Name="lbHistory" VerticalAlignment="Top" Width="460" ItemsSource="{Binding historyItemsCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,10">*</TextBlock>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding VehicleName}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<!--<TextBlock x:Name="ocDate" Text="{Binding OilChangedDate}"></TextBlock>-->
<TextBlock Text="{Binding OilChangedDate, Converter={StaticResource dateTimeConverter},ConverterParameter=\{0:M\}}" />
<TextBlock x:Name="ocOdometer" Text="{Binding OilChangedOdometer}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</ListBox>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/check.png" Text="save" x:Name="btnSave" Click="btnSave_Click"/>
<shell:ApplicationBarIconButton IconUri="/Images/cancel.png" Text="cancel" x:Name="btnCancel" Click="btnCancel_Click"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>

I also had a bit of trouble with converters and found an easy work around is to have another property on the class you are binding called something along the lines of DisplayDate.
For example if you are binding OilChangedDate you could add this property to the class you bind:
public string OilChangedDisplayDate
{
get { return OilChangedDate.ToShortDateString(); }
}
Then bind this property instead of the date directly.

Related

C# WPF Prism RegionManager failed to navigate

Using WPF .Net Core 3.1 + Prism.
I've implemented TabControl with dynamically added TabItems.
RegionManager works just fine when trying to navigate to region within MainWindow, but it failed when trying to navigate to region whithin the TabPage. Does not work neither from MainWindowViewModel, nor from TabPageViewModel.
Project's repo may be found here: GitHub.
MainWindow.xaml:
<TabControl Margin="10" ItemsSource="{Binding TabPages, UpdateSourceTrigger=PropertyChanged}">
<TabControl.Resources>
<DataTemplate DataType="{x:Type viewmodels:SomeTabPageViewModel}">
<views:SomeTabPage/>
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemTemplate>
<DataTemplate DataType="{x:Type interfaces:ITabPage}" x:Name="dt">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="40">
<TextBlock Grid.Column="1" Margin="5,10,10,10" Text="{Binding TabName}" TextAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
</Grid>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
SomeTabPage.xaml:
<UserControl x:Class="WpfPrismRegionFailure.Views.SomeTabPage"
xmlns:prism="http://prismlibrary.com/"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Navigate From Inside Of TabPage (not working)" Command="{Binding NavigateFromInsideOfTabPageCommand}" FontSize="16" Background="Orange"/>
<TextBlock Grid.Row="1" Margin="10,20,0,0" Text="The area below contains region 'RegionInsideTabPage', but can't navigate to it." FontSize="16"/>
<Border Grid.Row="2" BorderBrush="Orange" BorderThickness="2" CornerRadius="10" Margin="10">
<ContentControl Margin="10" prism:RegionManager.RegionName="RegionInsideTabPage"/>
</Border>
</Grid>
</UserControl>
SomeTabPageViewModel.cs:
using Prism.Commands;
using Prism.Ioc;
using Prism.Regions;
using System.Windows.Input;
using WpfPrismRegionFailure.Base;
namespace WpfPrismRegionFailure.ViewModels
{
public class SomeTabPageViewModel : TabPageBase
{
public SomeTabPageViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
this.NavigateFromInsideOfTabPageCommand = new DelegateCommand(OnNavigateFromInsideOfTabPage);
}
public SomeTabPageViewModel(IRegionManager regionManager) : base (regionManager)
{
this.NavigateFromInsideOfTabPageCommand = new DelegateCommand(OnNavigateFromInsideOfTabPage);
}
public ICommand NavigateFromInsideOfTabPageCommand { get; }
private void OnNavigateFromInsideOfTabPage()
{
System.Diagnostics.Trace.WriteLine($"SomeTabPageViewModel _regionManager ContainsRegionWithName RegionInsideTabPage: {this._regionManager.Regions.ContainsRegionWithName("RegionInsideTabPage")}");
this._regionManager.RequestNavigate("RegionInsideTabPage", "RegionContent");
}
}
}
Inside of TabPageViewModel regionManager.Regions.ContainsRegionWithName("RegionInsideTabPage") returns 'false' despite of the region is being declared in XAML
<ContentControl Margin="10" prism:RegionManager.RegionName="RegionInsideTabPage"/>
Project's repo may be found here: GitHub.
Someone heeeeeeelp me please

Image not displaying in UserControl

I've found so many answers to this questions, but no matter what I try nothing seems to work.
I'm trying to create a UserControl in a UWP Application. The User Control consists of a Border, with an Image and a TextBlock inside of it. I managed to get the TextBlock text to show up, but no matter what I do I can't seem to get an Image to display in the UserControl.
Things tried so far:
Changing GraphicProperty typeof to Image, ImageSource, and Uri.
Placing the Image in the Border into a ContentPresenter.
Other things I can't remember at this point.
I'm at a loss as to how to get this to work. I know I've done this before, but it was several years ago and I've apparently fogotten how (or something significant changed with UWP, as most of my experience is in WPF).
Could someone please help find where I'm messing this up?
UserControl XAML:
<UserControl
x:Class="ShirtSleeves.CardControlxaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ShirtSleeves"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="CardControl"
mc:Ignorable="d"
d:DesignHeight="400"
d:DesignWidth="300">
<Grid>
<Border Margin="0,10" Background="White" BorderBrush="Black" BorderThickness="5" Width="260" Height="352" CornerRadius="20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Source="{Binding ElementName=CardControl, Path=Graphic}" Height="200" Width="200" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ElementName=CardControl, Path=Label}" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,30" Foreground="Black" FontSize="48" FontWeight="Bold" />
</Grid>
</Border>
</Grid>
</UserControl>
UserControl C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
namespace ShirtSleeves
{
public sealed partial class CardControlxaml : UserControl
{
//private TextBlock label;
public static DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string),
typeof(CardControlxaml), new PropertyMetadata("Label"));
public static DependencyProperty GraphicProperty = DependencyProperty.Register("Graphic", typeof(string),
typeof(CardControlxaml), new PropertyMetadata(null));
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
public string Graphic
{
get { return (string)GetValue(GraphicProperty); }
set { SetValue(GraphicProperty, value); }
}
public CardControlxaml()
{
this.InitializeComponent();
}
}
}
MainPage XAML:
<Page
x:Class="ShirtSleeves.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ShirtSleeves"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="1200"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Background="DarkGreen">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="0">
<StackPanel>
<Border Margin="0,10" Background="White" BorderBrush="Black" BorderThickness="5" Width="260" Height="352" CornerRadius="20">
<TextBlock Text="Games" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,30" Foreground="Black" FontSize="48" FontWeight="Bold" />
</Border>
<Border Margin="0,10" Background="White" BorderBrush="Black" BorderThickness="5" Width="260" Height="352" CornerRadius="20">
<TextBlock Text="Sleeves" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,30" Foreground="Black" FontSize="48" FontWeight="Bold" />
</Border>
<local:CardControlxaml Label="Search" Graphic="C:\Users\<username>\source\repos\ShirtSleeves\ShirtSleeves\Images\Rook (Games).png" Foreground="Black" />
<Image Height="250" Source="C:\\Users\\<username>\\source\\repos\\ShirtSleeves\\ShirtSleeves\\Images\\Rook (Games).png" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</ScrollViewer>
<Viewbox Grid.Column="1">
<Image Source="C:\Users\<username>\source\repos\ShirtSleeves\ShirtSleeves\Images\Rook (Games).png" VerticalAlignment="Top" HorizontalAlignment="Center" />
</Viewbox>
</Grid>
</Page>
UWP doesn't allow to access files outside the app container directly. This means that you could not set the image source like this C:\Users\<username>\source\repos\ShirtSleeves\ShirtSleeves\Images\Rook (Games).png.
In your case, the simplest way is to put the image into your project's Assets folder like the following:
Then, you could specify the 'Graphic' property like this:
<local:CardControlxaml Label="Search" Graphic="Assets/animals.jpg" Foreground="Black" />
More information, please read File access permissions.

Windows Phone 7.1 emulator does not load application

I'm currently working on my first application targeting Windows Phone 7.1 and can't seem to get it running in the emulator. The project builds successfully, but when I debug the emulator screen stays black. No exceptions seem to be thrown and I can get the program to break on breakpoints, though no variables appear in the Locals window. I've tried each of the available emulators with no luck:
Windows Phone Emulator - 512 MB
Windows Phone Emulator - 256 MB
Windows Phone Emulator 7.8 512 MB
Windows Phone Emulator 7.8 256 MB
Another post on the Interwebz said to make sure the Build and Deploy boxes are both checked in the Configuration Manager, and mine are.
My MainPage.xaml isn't anything crazy:
<phone:PhoneApplicationPage
x:Class="NationalParkGuide.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="False">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Panorama control-->
<controls:Panorama Title="national park guide">
<controls:Panorama.Background>
<ImageBrush ImageSource="Yosemite.jpg" />
</controls:Panorama.Background>
<!--Panorama item one-->
<controls:PanoramaItem Header="menu">
<!--Double line list with text wrapping-->
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
<!--Panorama item two-->
<!--Use 'Orientation="Horizontal"' to enable a panel that lays out horizontally-->
<controls:PanoramaItem Header="second item">
<!--Double line list with image placeholder and text wrapping-->
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
</controls:Panorama>
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs hasn't been modified from auto-generated code yet:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace NationalParkGuide
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
DataContext = App.ViewModel;
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
}
}
Help! :)
Additional Information
I ran dxdiag.exe and can confirm I meet the requirements for the 7.1 emulator. DirectX 11 and WDDM 1.1.
Check if you don't have already 3 apps deployed on the emulator

Why I can't bind the Command to the button?

I work on a project target on Windows Phone 7.5 and above.
What I have
ListBox
<ListBox HorizontalAlignment="Left"
VerticalAlignment="Top"
SelectedItem="{Binding singleFavListItem, Mode=TwoWay}"
ItemTemplate="{StaticResource userFavBoardListItemTemplate}"
ItemsSource="{Binding userfavboardlist}"
ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="12,0,0,12"/>
ItemTemplate
<DataTemplate x:Key="userFavBoardListItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left"
TextWrapping="Wrap"
Text="{Binding boardName}"
VerticalAlignment="Center"
FontSize="{StaticResource PhoneFontSizeMedium}"
Foreground="{StaticResource TitleColor}"/>
<Button Command="{Binding quitBoardCommand}"
CommandParameter="{Binding boardUrl}"
Content="Quit"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Column="1"
FontSize="{StaticResource PhoneFontSizeSmall}"
BorderBrush="{StaticResource DateArticalCategoryColor}"
Foreground="{StaticResource DateArticalCategoryColor}">
</Button>
</Grid>
</DataTemplate>
ViewModel
public MyFavListViewModel()
{
this._quitBoardCommand = new DelegateCommand(this.quitBoardAction);
}
private ICommand _quitBoardCommand;
public ICommand quitBoardCommand
{
get
{
return this._quitBoardCommand;
}
}
private void quitBoardAction(object p)
{
//my business logic here
}
Error
I found a error in the OutPut windows:
'xicihutong.Model.UserFavBoardListRawData' (HashCode=55845053).
BindingExpression: Path='quitBoardCommand'
DataItem='xicihutong.Model.UserFavBoardListRawData'
(HashCode=55845053); target element is
'System.Windows.Controls.Button' (Name=''); target property is
'Command' (type 'System.Windows.Input.ICommand')..
What's the problem
What confuse me is that the quitBoardCommand never get triggered when I tap the button? It seems that I can't bind the Command to the button, the DelegateCommand part is right, because I can use it to bind command in other pages. And the SelectedItem of the ListBox works right, also.
Why I can't bind this one?
You need to reference the DataContext of your ListBox to bind to your command. To fix this, give your ListBox a name then reference the command property
<ListBox x:Name="myLB"
<!-- rest of your stuff -->
/>
<Button
Command="{Binding Path=DataContext.quitBoardCommand, ElementName=myLB}"
CommandParameter="{Binding boardUrl}"
Content="Quit" />
Can you try this code. You don't forget to change "datacontext" name.
<DataTemplate x:Key="userFavBoardListItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left"
TextWrapping="Wrap"
Text="{Binding boardName}"
VerticalAlignment="Center"
FontSize="{StaticResource PhoneFontSizeMedium}"
Foreground="{StaticResource TitleColor}"/>
<Button Command="{Path=quitBoardCommand,Source={StaticResource datacontext}}"
CommandParameter="{Binding boardUrl}"
Content="Quit"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Column="1"
FontSize="{StaticResource PhoneFontSizeSmall}"
BorderBrush="{StaticResource DateArticalCategoryColor}"
Foreground="{StaticResource DateArticalCategoryColor}">
</Button>
</Grid>
</DataTemplate>

Can't access LayoutRoot: Cannot implicitly convert type 'System.Windows.UIElement' to 'System.Windows.Controls.Grid'

I am trying to access LayoutRoot children using this simple code
using System.Windows.Controls;
using Microsoft.Phone.Controls;
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Grid grid = this.LayoutRoot.Children[1];
}
}
}
But get this error:
Cannot implicitly convert type 'System.Windows.UIElement' to
'System.Windows.Controls.Grid'. An explicit conversion exists (are you
missing a cast?)
XAML code
<phone:PhoneApplicationPage x:Class="PhoneApp2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"
Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot"
Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION"
Style="{StaticResource PhoneTextNormalStyle}"
Margin="12,0" />
<TextBlock Text="page name"
Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
</Grid>
</Grid>
</phone:PhoneApplicationPage>
What am I doing wrong?
try this
Grid grid = this.LayoutRoot.Children[1] as Grid;
Error is coming because this.LayoutRoot.Children[1] returns object of UIElement which is base class of many WPF controls for example Grid
What you need to do is explicitly convert the UIElement to Grid using as

Categories

Resources