我有许多 XAML TextBlock,如下所示:
<TextBlock Text="{Binding InspectionCount}" Style="{StaticResource FilterText}">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding SetModeToAll}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
我想减少 XAML 代码量并编写如下内容:
<MyTextBlock Text="{Binding InspectionCount}"
Style="{StaticResource FilterText}"
Command="{Binding SetModeToAll}" />
Run Code Online (Sandbox Code Playgroud)
我只知道执行此类操作的一种方法 - 创建新类(从 TextBlock 继承),将 AttachedProperty(命令)添加到新类,并为新类创建样式。
但看起来我无法从 TextBlock 继承我的类:
public sealed class FilterTextBlock : TextBlock
Run Code Online (Sandbox Code Playgroud)
无法从密封类 TextBlock 继承
为什么我做不到?可能还有其他方法可以实现我的愿望吗?
我发现以下方法有效。并解决了很多地方的问题,我将开始回答,尝试以下!
<Page
x:Class="Example1.ListBoxTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Example1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
mc:Ignorable="d">
<Page.Resources>
<i:BehaviorCollection x:Key="behaviors">
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding SetModeToAll}" />
</core:EventTriggerBehavior>
</i:BehaviorCollection>
<Style TargetType="TextBlock" x:Key="textblockstyle">
<Setter Property="i:Interaction.Behaviors" Value="{StaticResource behaviors}">
</Setter>
</Style>
</Page.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Text="Testing" Foreground="Red" FontSize="20" Style="{StaticResource textblockstyle}">
</TextBlock >
</Grid>
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我在属性 i:Interaction.Behaviors 中设置行为,则不起作用,但以这种方式调用命令!请告诉我这对你也有效!
| 归档时间: |
|
| 查看次数: |
2338 次 |
| 最近记录: |