Styling TimePicker control - windows-phone-7.1

I'm trying style the TimePicker control that appears on the form so that the text colour changes accordingly to the phone's style with a transparent background. For normal text, I just use:
Style="{StaticResource PhoneTextNormalStyle}"
However, this won't work for the TimePicker and I get an error when I try to build the project.
Sorry for the severe lack of detail right now, I current don't have access to my development box right now. When I get home I'll post a screenshot of the control and the error.

The easy way to do this would be in Expression Blend. Open up the project in there, right-click on the control and select "Edit Style" -> "Edit a copy". This will put a style element into the page.resources section that looks like the following snippet
<Style x:Key="TimePickerStyle1" TargetType="toolkit:TimePicker">
<Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="PickerPageUri" Value="/Microsoft.Phone.Controls.Toolkit;component/DateTimePickers/TimePickerPage.xaml"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="toolkit:TimePicker">
<StackPanel>
<ContentControl ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Foreground="{StaticResource PhoneSubtleBrush}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="12,0,12,-4"/>
<Button x:Name="DateTimeButton" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Content="{TemplateBinding ValueString}" Foreground="{TemplateBinding Foreground}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Height="72"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and your control definition will look like this:
<toolkit:TimePicker Name="TimePickerInstance" Style="{StaticResource TimePickerStyle1}"></toolkit:TimePicker>
Of course, you can put this snippet in there yourself, and reference it in the same way in the control. It will all work the same way - Expression Blend just makes the process of doing it much simpler.
You should be able to manipulate that as you need to from there, probably changing the 2nd and 4th lines to
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
Hope that helps.

Related

Xamarin.Forms custom font not picked up on iOS device when FontAttributes specified

I'm using a custom font in our app, and have found that if I set a FontAttributes="Bold" for a Label style using the font, it works on the iOS simulator, but not on a real device.
Steps taken:
1. Added the font Effra_Std_Reg.ttf to the resources folder.
2. Added this to the Fonts Provided By The Application element in Info.plist and set its build actions.
3. Created Styles in App.XAML.
4. Added these styles to the XAML on my page.
All the above works fine for a Label with no FontAttributes set. As soon as I set FontAttributes="Bold" on either the Style or the Label itself, the system font is used insted. Note that this works on the Simulator.
I know that the FontFamily is the font Name as defined inside the ttf file - not the ttf filename.
This on an iPad running iOS 10.3.3.
My App.XAML is:
<?xml version="1.0" encoding="utf-8" ?>
<Application x:Class="FontTest.App"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Label">
<Setter Property="FontSize" Value="30" />
</Style>
<Style x:Key="PlainLabelStyle" TargetType="Label">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String"
Android="Effra_Std_Rg.ttf#Effra"
iOS="Effra" />
</Setter.Value>
</Setter>
</Style>
<Style x:Key="BoldLabelStyle" TargetType="Label">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String"
Android="Effra_Std_Bd.ttf#Effra"
iOS="Effra" />
</Setter.Value>
</Setter>
<Setter Property="FontAttributes" Value="Bold" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
And my page XAML (extract) is:
<Label Grid.Row="4"
Grid.Column="1"
Style="{StaticResource BoldLabelStyle}"
Text="QWERTYUIOPASDFGHJKLZXCVBNM" />
To get it working on the device, I had to include the bold font file (Effra_Std_Bd.ttf), and specify a FontFamily of 'Effra-Bold'.
So it appears that the FontFamily needs to be the Name and the Style.
Strange that it worked on the Simulator.

Get click event of a button placed in App.xaml

I have a style in my App.xaml page as shown below
<Style x:Key="ListBoxMore" TargetType="ListBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
<StackPanel>
<ItemsPresenter/>
<Button Content="Load More..." Click="btnLoadMore_Click" Name="btnLoadMore" Visibility="Visible" Background="White" Foreground="Black"></Button>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I am using this style in 4 different pages. I need to get the click event of the "Load More" button in those pages. How can I get this in my windows phone application?
You can't.
Instead, try using a Command which you can bind to the ViewModel.
Alternatively, turn your restyled listbox into a custom control and then you can have a centralised eventhandler there.

Wpf animation - like shaking icons on iOS

I would like to have an image animation in wpf where the image shakes lightly in order to delete it, as it happens for apps in iOS.
Do you know something that would help ? Something that already built?
Thanks a lot.
Here's a complete sample of shaking the text of button. You should be able to adapt this to shake an image, and improve it using easing functions.
<Grid.Resources>
<ControlTemplate x:Key="ShakingButtonTemplate" TargetType="Button">
<Border Margin="5" BorderBrush="Aquamarine" BorderThickness="5" CornerRadius="5">
<ContentPresenter HorizontalAlignment="Center" Content="{TemplateBinding Content}">
<ContentPresenter.RenderTransform>
<TransformGroup>
<TranslateTransform x:Name="Position"/>
</TransformGroup>
</ContentPresenter.RenderTransform>
</ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="ShakeIt">
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="Position"
Storyboard.TargetProperty="X"
RepeatBehavior="5x"
>
<EasingDoubleKeyFrame KeyTime="0:0:0.05" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="2"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.20" Value="-2"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<!--
<DoubleAnimation
Storyboard.TargetName="Position"
Storyboard.TargetProperty="X"
From="-2" To="2"
Duration="0:0:0:0.1"
AutoReverse="True"
RepeatBehavior="10x">
</DoubleAnimation>
-->
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="ShakingButton" TargetType="Button">
<Setter Property="Template" Value="{StaticResource ShakingButtonTemplate}"/>
</Style>
</Grid.Resources>
<StackPanel>
<Button Style="{StaticResource ShakingButton}" Content="This is a button" />
</StackPanel>

Tilt Effect for HubTile

I added the TiltEffect.IsTiltEnabled property to my HubTile:
<toolkit:HubTile toolkit:TiltEffect.IsTiltEnabled="True" Title="title" Message="This is message" x:Name="name" DisplayNotification="False" Source="pB.png" Tap="tap" />
I also added the HubTile type to the TiltableItems collection in the page's constructor, per the :
public HubPage()
{
TiltEffect.TiltableItems.Add(typeof(HubtTile));
}
but the HubTile don't have tilt effect....
Thanks!
Try adding your HubTile as Content of HyperLinkButton or Button which supports TiltEffect
Add a Style to PhoneApplicationPage.Resources which works for Button and HyperLinkButton that adds support for other controls as Content
<phone:PhoneApplicationPage.Resources>
<Style x:Key="EmptyButtonStyle" TargetType="primitives:ButtonBase">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Padding" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:ButtonBase">
<Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
Also add the primitives namespace to the page
xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows"
Then use your HubTile as HyperLinkButtons's (or Button) Content like this
<HyperlinkButton Style="{StaticResource EmptyButtonStyle}">
<toolkit:HubTile toolkit:TiltEffect.IsTiltEnabled="True" Title="title" Message="This is message" x:Name="name" DisplayNotification="False" Source="pB.png" Tap="tap" />
</HyperlinkButton>
This will finally support TiltEffect and HubTile
Get TiltEffect from Updated Tilt Effect - Peter Torr's Blog
OR download the Silverlight Toolkit Source from Silverlight Toolkit - Downloads
and then add the HubTile class to the list of 'tiltableItems' in TiltEffect.cs:
/// <summary>
/// Default list of items that are tiltable
/// </summary>
static List<Type> tiltableItems = new List<Type>() { typeof(ListBoxItem) , typeof(HyperlinkButton) , typeof(HubTile) };
If you decide to modify the Toolkit, be sure to build the project on Release, and target the .dll you create inside your own application.
I've found other simple solution that works great when HubTile is used as ListBox.ItemTemplate.
Simply set TiltEffect.IsTiltEnabled="True" on ListBox and IsHitTestVisible="False" on HubTile and voila - it works :) You can still respond to HubTile clicks through ListBox.SelectionChanged event and HubTile has working tilt effect.
Hubtile by default is not added to TiltableItems List and needs to be added manullay...
Add the following code to codebehind
public MainPage()
{
InitializeComponent();
TiltEffect.TiltableItems.Add(typeof(HubTile));
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
and add the following line to your hubtile or grid containing the hub tile
<phone:PanoramaItem CacheMode="{x:Null}" Header="item3" toolkit:TiltEffect.IsTiltEnabled="True">

WPF Binding Button.Command in ControlTemplate to property of ViewModel

My ViewModel has a property called Commands which is of type IDictionary.
For my data grid I have created a ControlTemplate for one of the fields using a button as follows:
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Button Style="{DynamicResource btnRemove}" Width="14" Height="14"
Content="{TemplateBinding Content} "
CommandParameter="{Binding ViewID}"
Command="{Binding Commands[AcknowledgeErrorCmd]}" />
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
Clicking on the button does nothing which tells me the binding did not work. However, an unstyled button added to the toolbar of the same window hosting this grid works, binds properly to this command. I guess my question is:
Hw do I bind the command property of a button used in a ControlTemplate to a ViewModel?
TIA.
I am not sure what the problem is but try to debug your solution and look into the output window with Debug selected in the combobox and you will see the errors that occur during binding. Maybe this will help you to the solution.
Provide me the error as a comment on this post if you don't understand it.
I did this instead:
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<Border >
<TextBlock Margin="5">
<Hyperlink
CommandParameter="{Binding ElementName=root, Path=DataContext.ViewID}"
Command="{Binding ElementName=root, Path=DataContext.Commands[AcknowledgeErrorCmd]}">
<TextBlock Text="Acknowledge"/>
</Hyperlink>
</TextBlock>
</Border>
</ControlTemplate>
and that works fine. It may be related to the post Viko provided.

Resources