将 wpf 触发器转换为 silverlight 的最佳方法?
我在 wpf 应用程序中开发了几个使用触发器的自定义控件。转换代码的最快方法是什么,以便我拥有可在 wpf 应用程序和 silverlight 应用程序中工作的单一代码库。这是代码示例:
<Style x:Key="sButtonAction" TargetType="Button">
<!--<Setter Property="BitmapEffect" Value="{StaticResource BannerEffect}" />-->
<Setter Property="Height" Value="25" />
<Setter Property="Margin" Value="4" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="PART_Border" CornerRadius="10"
BorderThickness="{StaticResource sBorderThicknessStandard}"
BorderBrush="{StaticResource bColorBorder}"
Background="{StaticResource ButtonActionBackground}">
<TextBlock x:Name="PART_TextBlock" Margin="5,2,5,2" HorizontalAlignment="Center"
VerticalAlignment="Center" Foreground="White">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" /></TextBlock>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_TextBlock" Property="Foreground"
Value="#990000"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="PART_Border" Property="Background"
Value="{StaticResource ButtonActionBackgroundSelected}"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I have developed several custom controls in a wpf application that use triggers. what is the fastest way to convert the code so that I have a single code base that works both in the wpf application and the silverlight applicaiton. Here is a sample of the code:
<Style x:Key="sButtonAction" TargetType="Button">
<!--<Setter Property="BitmapEffect" Value="{StaticResource BannerEffect}" />-->
<Setter Property="Height" Value="25" />
<Setter Property="Margin" Value="4" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="PART_Border" CornerRadius="10"
BorderThickness="{StaticResource sBorderThicknessStandard}"
BorderBrush="{StaticResource bColorBorder}"
Background="{StaticResource ButtonActionBackground}">
<TextBlock x:Name="PART_TextBlock" Margin="5,2,5,2" HorizontalAlignment="Center"
VerticalAlignment="Center" Foreground="White">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" /></TextBlock>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_TextBlock" Property="Foreground"
Value="#990000"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="PART_Border" Property="Background"
Value="{StaticResource ButtonActionBackgroundSelected}"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管听起来很痛苦,但更改触发器以使用 VisualStateManager 是最好的选择。它包含在 WPF 4.0 中,如果您坚持使用早期版本,可以从 Codeplex 获取它。
Silverlight 不支持触发器很糟糕,但如果您需要支持这两个平台,我认为这是您最好的选择。
As painful as it sounds, changing your triggers to use the
VisualStateManager
is your best bet. It is included in WPF 4.0, and if you are stuck on an earlier version, you can grab it off of codeplex.It sucks that Silverlight doesn't support triggers, but if you need to support both platforms, I think that is your best bet.