Set a Style Programmatically : Style « Windows Presentation Foundation « C# / C Sharp






Set a Style Programmatically

Set a Style Programmatically
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="Window_Loaded" Title="WPF" Height="230" Width="140">
    <Window.Resources>
        <Style x:Key="labelStyle1">
            <Setter Property="Label.Background" Value="LightYellow" />
            <Setter Property="Label.HorizontalContentAlignment" Value="Center" />
        </Style>
        <Style x:Key="imageStyle1">
            <Setter Property="Image.Source" Value="c:\image.png" />
            <Setter Property="Image.Height" Value="140" />
            <Setter Property="Image.Width" Value="96" />
        </Style>
        <Style x:Key="labelStyle2">
            <Setter Property="Label.Background" Value="AliceBlue" />
            <Setter Property="Label.Foreground" Value="DarkBlue" />
        </Style>
        <Style x:Key="imageStyle2">
            <Setter Property="Image.Source" Value="c:\image.png" />
            <Setter Property="Image.Height" Value="140" />
            <Setter Property="Image.Width" Value="96" />
        </Style>
    </Window.Resources>
    <StackPanel>
        <Image x:Name="img"/>
        <Label x:Name="lbl" Content="Hello" />
    </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lbl.Style = (Style)FindResource("labelStyle2");
            img.Style = (Style)FindResource("imageStyle2");
            //lbl.Style = (Style)FindResource("labelStyle1");
            //img.Style = (Style)FindResource("imageStyle1");

        }
    }
}

   
    
  








Related examples in the same category

1.Double value as the Font sizeDouble value as the Font size
2.FontFamily resource constantFontFamily resource constant
3.Styles With Same KeysStyles With Same Keys
4.Setter With Binding to SliderSetter With Binding to Slider
5.Style With Data TriggerStyle With Data Trigger
6.Styles With Target TypesStyles With Target Types
7.Style With Multiple ElementsStyle With Multiple Elements
8.Style With Multiple ButtonsStyle With Multiple Buttons
9.Style With ResourceStyle With Resource
10.Style With Property ElementStyle With Property Element
11.Style With TriggersStyle With Triggers
12.Style Slider: Background, IsSnapToTickEnabled, AutoToolTipPlacement, TickFrequencyStyle Slider: Background, IsSnapToTickEnabled, AutoToolTipPlacement, TickFrequency
13.Normal: Origin at upper leftNormal: Origin at upper left
14.Stroke Start Line CapStroke Start Line Cap
15.Property TriggerProperty Trigger
16.Using a Style resourceUsing a Style resource
17.Sharing a StyleSharing a Style
18.Ignore an Implicit Style by setting Style to NullIgnore an Implicit Style by setting Style to Null
19.extends StyleSelector to create your own style selectorextends StyleSelector to create your own style selector
20.The default GroupStyle indents the items in a groupThe default GroupStyle indents the items in a group