May 30, 2011

Code Example to set Style on WPF Browser Application/XBAP Application

To Create WPF Browser Application/XBAP Application Go to New Project in VS2008 or later versions
Select Template WPFBrowserApplication and Click OK.



This will create a XBAP(XAML Browser Application) with some files
page.xaml(Design Page Here), App.xaml(Application configurations here)

2.)To add a new page Right click On Solution Explorer and from new Click on Page, Give Name and add it in the Application


3.) Below is the sample code To set Style on a page  (A style consists of a list of setters)


ExStyle2.xaml

<Page x:Class="TestWPF.ExStyle2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ExStyle2">


    <StackPanel Orientation="Vertical"  VerticalAlignment="Top">
        <Button Style="{StaticResource myStyle}" Height=" 50" Width="50">SEE</Button>
        <Button Style="{StaticResource myStyle}" Height=" 50" Width="50">WPF</Button>
        <Button Style="{StaticResource myStyle}" Height=" 50" Width="50">STYLES</Button>
        </StackPanel>
  
    
</Page>

Here in the above code Style setter is myStyle which is defined in App.xaml 


 App.xaml

<Application x:Class="TestWPF.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="ExStyle2.xaml">
    <Application.Resources>
        <Style x:Key="myStyle" TargetType="Button">
            <Setter Property="Background" Value="Orange" />
            <Setter Property="FontStyle" Value="Italic" />
            <Setter Property="Padding" Value="8,4" />
            <Setter Property="Margin" Value="4" />
        </Style>
        
        <Style x:Key="DialogButtonStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse Fill="{TemplateBinding Background}"
                             Stroke="{TemplateBinding BorderBrush}"/>
                            <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>


StartupUri attribute is used to set the StartUpPage of the App.






Run the app an see the Style applied to the page


No comments:

Post a Comment