【问题标题】:Appbar button color not persisting till onclick function returnsAppbar 按钮颜色不会持续到 onclick 函数返回
【发布时间】:2016-12-18 05:30:21
【问题描述】:

单击联系人图标后,其背景颜色会变为蓝色,但会立即恢复为原始颜色。我想将背景颜色保持为蓝色,直到 OnClick 函数返回。不知道有什么办法吗?

<Page.BottomAppBar>
        <CommandBar>
            <CommandBar.PrimaryCommands>
                <AppBarButton x:Name="ContactBtn" x:Uid="Contact_id" Icon="Contact" Label="View Contacts" Click="OnClick" />
            </CommandBar.PrimaryCommands> 
       </CommandBar>
</Page.BottomAppBar>

【问题讨论】:

    标签: c# windows xaml button appbar


    【解决方案1】:

    按钮的样式取决于它的 VisualState。我认为最简单的方法是使用 AppBarToggleButton 并在执行期间将其设置为 IsCheckedtrue
    Click 会自动将其设置为 true,因此您只需将其重置为 false

    但这仍然不像听起来那么容易。因为这仅在您在单击事件中执行一些异步操作或打开Popup 或类似内容时才有效。

    如果您只执行同步操作,按钮 UI 将不会更新。所以我认为它已经是异步的了。

    【讨论】:

    • 非常感谢...您的解决方案对我有用。使用 AppBarToggleButton 并设置-重置它的 IsChecked 属性解决了我的问题。
    • @SanjeevKumar 很高兴它有帮助。如果您对我的帖子进行投票和/或选择它作为答案,那就太好了。
    【解决方案2】:

    在 OnClick 方法开始时将 AppBarButton 的属性 IsPressed 设置为 true,最后设置为 false;

    编辑

        <interactivity:Interaction.Behaviors>
        <core:DataTriggerBehavior Binding="{Binding Path=IsPressed, RelativeSource={RelativeSource Self}" Value="True">
            <core:ChangePropertyAction PropertyName="Background" Value="Blue"/>
        </core:DataTriggerBehavior>
        <core:DataTriggerBehavior Binding="{Binding Path=IsPressed, RelativeSource={RelativeSource Self}" Value="False">
            <core:ChangePropertyAction PropertyName="Background" Value="Black"/>
        </core:DataTriggerBehavior>
    </interactivity:Interaction.Behaviors>
    

    当 IsPressed 改变时,你可以设置你想要的背景。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2012-05-02
    • 2015-01-12
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多