UWP - 在加载页面时使用 EventTriggerBehavior 和 ChangePropertyAction 更改 属性 值
UWP - Changing a property value using EventTriggerBehavior and ChangePropertyAction when the page is loaded
我有一个包含主要细节模式的页面。在左侧,有一个 ListView,在右侧,有一些 TextBlocks 显示有关 ListView 的 SelectedItem 的信息。加载页面时,开始时没有任何选择项,因此右侧的文本块是空白的。但是我想在没有任何选定项目时显示一些初始文本。所以我做了类似下面的事情(为了清楚起见,我隐藏了一些代码):
<Page>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:ChangePropertyAction TargetObject="{Binding ElementName=Description}" PropertyName="Text" Value="It works!" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
........
<TextBlock x:Name="Description" Text="{x:Bind ViewModel.SelectedItem.Description, Mode=OneWay}" />
</Page>
但是没用。文本块保持空白。我怎样才能达到我想要的?
顺便说一句,我正在遵循 MVVM 模式,因此我正在尝试尽可能减少代码隐藏,但如果没有其他选择,我会将该逻辑放在代码隐藏中。
您需要将 ChangePropertyAction
附加到您的 TextBlock。页面的加载并不能保证 TextBlock 的加载。
<TextBlock ...>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:ChangePropertyAction TargetObject="{Binding ElementName=Description}" PropertyName="Text" Value="It works!" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</TextBlock>
我有一个包含主要细节模式的页面。在左侧,有一个 ListView,在右侧,有一些 TextBlocks 显示有关 ListView 的 SelectedItem 的信息。加载页面时,开始时没有任何选择项,因此右侧的文本块是空白的。但是我想在没有任何选定项目时显示一些初始文本。所以我做了类似下面的事情(为了清楚起见,我隐藏了一些代码):
<Page>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:ChangePropertyAction TargetObject="{Binding ElementName=Description}" PropertyName="Text" Value="It works!" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
........
<TextBlock x:Name="Description" Text="{x:Bind ViewModel.SelectedItem.Description, Mode=OneWay}" />
</Page>
但是没用。文本块保持空白。我怎样才能达到我想要的?
顺便说一句,我正在遵循 MVVM 模式,因此我正在尝试尽可能减少代码隐藏,但如果没有其他选择,我会将该逻辑放在代码隐藏中。
您需要将 ChangePropertyAction
附加到您的 TextBlock。页面的加载并不能保证 TextBlock 的加载。
<TextBlock ...>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:ChangePropertyAction TargetObject="{Binding ElementName=Description}" PropertyName="Text" Value="It works!" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</TextBlock>