使用xaml和c#将两个样式添加到Windows应用商店应用中的按钮

时间:2014-09-24 21:49:20

标签: c# xaml windows-store-apps

我想在使用XAML和c#的windows store app中使用两个样式的按钮,在click事件上想要更改样式。我怎样才能做到这一点 ?我正在尝试添加资源但无法获得任何结果。请帮帮我。

这是我的app.xaml

<Application
    x:Class="App1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1">

    <Application.Resources>
        <Style TargetType="Button" x:Key="buttonStyle1">
            <Setter Property="Foreground" Value="Yellow" />
        </Style>

        <Style TargetType="Button" x:Key="buttonStyle2">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </Application.Resources>
</Application>

mainpage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        /// 
        private void EventButton(object sender, RoutedEventArgs e)
        {


        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}

和mainpage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <Button Name="ControlButton" Style="{StaticResource buttonStyle1}" Content="Button" Click="EventButton"/>
    </Grid>
</Page>

告诉我该怎么做 EventButton方法

1 个答案:

答案 0 :(得分:0)

ControlButton.Style = Application.Current.Resources["buttonStyle2"] as Style