0

Ok, so I know a little bit about GUI development, and am new to Windows 8 (Modern UI) I'd also like to do this with C# (no XAML) if possible.

Style What I'm trying to do here is create a style I can apply to a button created somewhere else.

    public static Style firstButtonStyle()
    {
        firstButton = new Style(typeof(Button));
        ControlTemplate btnControl = new ControlTemplate();

        //firstButton.Setters.Add(new Setter(Button.TemplateProperty, btnControl));
        firstButton.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Blue)));
        firstButton.Setters.Add(new Setter(Button.IsPointerOverProperty, new SolidColorBrush(Windows.UI.Colors.PaleGreen)));
        firstButton.Setters.Add(new Setter(Button.IsPressedProperty, Windows.UI.Colors.Beige));
        firstButton.Setters.Add(new Setter(Button.ForegroundProperty, Windows.UI.Colors.Red));

        return firstButton;
    }

Application

This is where the button is created and the style applied.

    private Button enterButtonCreation(string text)
    {
        Button enterButton = new Button();
        enterButton.Content = text;
        enterButton.Margin = new Thickness(200, 80, 20, 0);
        Style firstButtonStyl = WikierStyle.firstButtonStyle();
        enterButton.Style = firstButtonStyl;
        enterButton.Background = new SolidColorBrush(Windows.UI.Colors.Silver);
        return enterButton;

    }

I can change the background silver using .Background, but when using .BackgroundProperty nothing seems to happen.

Trevor Tiernan
  • 392
  • 1
  • 3
  • 9

1 Answers1

0

first of i suggest you that you should try to do all of your style work in your xaml file like try to declare custum styles in your xaml page. if you want to use the custumstyle in particularpage only then define it your page only like this. and you can apply this style on any button.

<Page
x:Class="Appgridcheck.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Appgridcheck"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
    <Style TargetType="Button" x:Name="CustomStyle" >
        <Setter Property="Background" Value="White" />
    </Style>
</Page.Resources>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Button Style="{StaticResource CustomStyle}" />
</Grid>

secondly if you want to define global custumStyle that you can use throughout your app(mean on all pages ) then define this style in app.xaml like this.

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

<Application.Resources>
    <ResourceDictionary>

        <Style TargetType="Button" x:Name="gllobalCustomstyle" >
            <Setter Property="Background" Value="Black" />
        </Style>

        <ResourceDictionary.MergedDictionaries>

            <!-- 
                Styles that define common aspects of the platform look and feel
                Required by Visual Studio project and item templates
             -->
            <ResourceDictionary Source="Common/StandardStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

now you use this tyle on any page..hope this will help..

loop
  • 9,002
  • 10
  • 40
  • 76
  • But if we get our requirements from a dictionary , and we do not know how many buttons are there in our app. So how to design a templet that can be reused in a button dynamically and changes its size accordingly. Please suggets – Sonu Aug 29 '13 at 11:09