我正在使用微软提供的TiltEffect util。我试图将它与TextBlocks一起使用,但是,即使我将TextBlock类型添加到可倾斜项列表中,它也不起作用:
TiltEffect.TiltableItems.Add( typeof( System.Windows.Controls.TextBlock ) );但是,如果我用一个TextBlock边框包围,并将Tap事件移动到边框,它就会正常工作。
这种行为有什么特别的原因吗?用边界包围所有可调用的TextBlocks并不太优雅。
更新:矩形形状也是如此。
UPDATE2:我的当前解决方案是一个解决方案,我定义了一个自定义控件:
<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>在后面的代码中:
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 ) ) ) );
}我目前不使用任何其他特定于TextBlock的属性,只使用文本,因此如果需要TextWrapping、FontSize等,则必须以同样的方式实现。
但是我对这个解决方案并不满意,所以仍然在寻找一个更优雅的解决方案。
UPDATE3:上面的方法并不完美,我发现它有一个“参数不正确”的异常(有时是在启动应用程序之后抛出的,有时不是)。
发布于 2012-02-17 10:36:18
我对Silverlight工具包倾斜效应并不满意,特别是它对基于类型的元素的“神奇”应用的方式。所以我写了一个替代方案。您还可以配置您想申请多少“倾斜”。源代码可以在这里找到:
地铁在动第4部分:倾斜效应
然后,您可以对元素单独应用如下:
<TextBlock local:MetroInMotion.Tilt="6"/> 其中整数指定要应用的倾斜度。我建议使用相当低的值,自然效果是相当微妙的,但是人们在自己的尾灯应用程序中往往过于极端,地铁效果应该是微妙的,他们不应该对你大喊大叫!
发布于 2013-08-27 00:04:21
万一别人发现了这件事,我有另一个解决办法。
只需将TextBlock元素用ListBoxItem标记括起来即可。
例如:
<ListBoxItem>
<TextBlock>
A Tilting Textblock
</TextBlock>
</ListBoxItem>发布于 2013-09-12 17:29:28
如果您想要TiltEfect,这可能意味着您正在使用它作为一个按钮,所以您应该使用按钮或HyperlinkButton,您重新样式,以显示只有文本。就像这样,您将能够利用按钮的额外属性,如Command。以下是超链接按钮样式的示例:
<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>您可以使用:
<HyperlinkButton Style="{StaticResource NoUnderlineHyperlinkButtonStyle}" Content="My Text" Tap="UIElement_OnTap"/>https://stackoverflow.com/questions/9326308
复制相似问题