UWP - 在加载页面时使用EventTriggerBehavior和ChangePropertyAction更改属性值

时间:2017-06-27 21:28:29

标签: uwp uwp-xaml

我有一个包含主细节模式的页面。在左侧,有一个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模式,所以我尽可能地减少了代码隐藏,但如果没有其他选择,我会把这个逻辑放在代码隐藏中

1 个答案:

答案 0 :(得分:0)

您需要将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>
相关问题