在onclick函数返回之前,Appbar按钮颜色不会持续存在

时间:2016-08-11 12:53:32

标签: c# windows xaml button appbar

点击联系人图标后,它的背景颜色变为蓝色,但会立即返回原始颜色。我想将背景颜色保持为蓝色,直到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>

2 个答案:

答案 0 :(得分:0)

在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发生变化时,您可以设置所需的背景。

答案 1 :(得分:0)

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

但这仍然不像听起来那么容易。因为如果您在点击事件中执行某些异步操作或打开Popup或类似事件,这只会产生影响。

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