4
votes

I am using the TiltEffect util provided by Microsoft. I am trying to use it with TextBlocks, however, it does not work, even if I add the TextBlock type to the list of Tiltable items:

TiltEffect.TiltableItems.Add( typeof( System.Windows.Controls.TextBlock ) );

However, if I surround the TextBlock with a Border, and move the Tap event to the Border, it works properly.

Is there any specific reason to this behaviour? It isn't too elegant to surround all my tappable TextBlocks with Borders.

UPDATE: The same applies to the Rectangle shape.

UPDATE2: My current solution is a workaround, I defined a custom control:

<UserControl x:Class="MyApp.Controls.TiltableTextBlock"
    Name="tiltableTextBlock"
    ...>
    <Button Margin="0" Padding="0">
        <Button.Template>
            <ControlTemplate>
                <TextBlock Margin="0" Padding="0"
                    Text="{Binding Text, ElementName=tiltableTextBlock}"
                    Style="{Binding Style, ElementName=tiltableTextBlock}" />
            </ControlTemplate>
        </Button.Template>
    </Button>
</UserControl>

And in the code behind:

public partial class TiltableTextBlock : UserControl
{
    public TiltableTextBlock()
    {
        InitializeComponent();
    }

    public string Text
    {
        get { return (string)GetValue( TextProperty ); }
        set { SetValue( TextProperty, value ); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register( "Text", typeof( string ), typeof( TiltableTextBlock ), new PropertyMetadata( String.Empty ) );

    //NOTE: hiding the Style property of the base class, so the Style does not get applied to the UserControl, rather to the TextBlock.
    public new Style Style
    {
        get { return (Style)GetValue( StyleProperty ); }
        set { SetValue( StyleProperty, value ); }
    }

    public new static readonly DependencyProperty StyleProperty =
        DependencyProperty.Register( "Style", typeof( Style ), typeof( TiltableTextBlock ), new PropertyMetadata( new Style( typeof( TextBlock ) ) ) );
}

I currently do not use any other TextBlock-specific property, just Text, so if TextWrapping, FontSize, etc is needed, it has to be implemented in the same way.

However I am not satisfied with this solution, so still looking for a more elegant workaround.

UPDATE3: The above approach is not perfect, I found out that it nondeterministically crashes with a "parameter is incorrect" Exception (sometimes it is thrown after I start the application, sometimes not).

3
I think this is related to my issue I just posted. Did you try making your background transparent? stackoverflow.com/questions/14664251/… - kamranicus

3 Answers

6
votes

I was not happy with the Silverlight toolkit tilt effect, especially the way that it is'magically' applied to elements based on type. So I wrote an alternative. You can also configure how much 'tilt' you want to apply. The sourcecode can be found here:

Metro in Motion Part #4: Tilt Effect

You can then individually apply tile to elements as follows:

<TextBlock local:MetroInMotion.Tilt="6"/> 

Where the integer specifies how much tilt to apply. I would recommend using quite low values, the native effect is quite subtle, however people tend to make it far too extreme in their own silerlight applications, Metro effects should be subtle, they should not shout at you!

5
votes

In case somebody else comes across this, I have another solution.

Just enclose the TextBlock element with ListBoxItem tags.

e.g.

<ListBoxItem>
   <TextBlock>
       A Tilting Textblock
   </TextBlock>
</ListBoxItem>
0
votes

If you want TiltEfect that probably mean that you are using it as a button so you should just use a Button or HyperlinkButton that you restyle to display just text. Like this you will be able to take advantage of the extra properties of the button like Command. Here is an example of Hyperlink button Style :

<Style x:Key="NoUnderlineHyperlinkButtonStyle" TargetType="HyperlinkButton">
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="HyperlinkButton">
                    <Border Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <!--<Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="TextElement">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>-->
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="TextElement">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border Background="{TemplateBinding Background}" Margin="{StaticResource PhoneHorizontalMargin}" Padding="{TemplateBinding Padding}">
                            <TextBlock x:Name="TextElement" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Text="{TemplateBinding Content}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

which you can use:

<HyperlinkButton Style="{StaticResource NoUnderlineHyperlinkButtonStyle}" Content="My Text" Tap="UIElement_OnTap"/>