Set a Style Programmatically : Style « Windows Presentation Foundation « VB.Net






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.vb

Imports System
Imports System.Windows

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
      lbl.Style = DirectCast(FindResource("labelStyle2"), Style)
      img.Style = DirectCast(FindResource("imageStyle2"), Style)
      'lbl.Style = (Style)FindResource("labelStyle1");
      'img.Style = (Style)FindResource("imageStyle1");

    End Sub
  End Class
End Namespace

   
    
    
  








Related examples in the same category

1.Property Trigger: mouse over and focusedProperty Trigger: mouse over and focused
2.Multi Data Trigger DemoMulti Data Trigger Demo
3.Button Based On Style with Target TypeButton Based On Style with Target Type
4.Button With Local StyleButton With Local Style
5.Graphics StylesGraphics Styles
6.Reuse Font With StylesReuse Font With Styles
7.Style InheritanceStyle Inheritance
8.Override Style PropertiesOverride Style Properties
9.Create Styles That Adapt to the Current OS ThemeCreate Styles That Adapt to the Current OS Theme
10.Set OverridesDefaultStyle to trueSet OverridesDefaultStyle to true
11.Inherit from a Common Base StyleInherit from a Common Base Style
12.Add any overriding property valuesAdd any overriding property values
13.Clear customized style with NullClear customized style with Null
14.Create a Named StyleCreate a Named Style
15.Applying a Consistent Margin with a StyleApplying a Consistent Margin with a Style
16.Style applied to a Button elementStyle applied to a Button element
17.Create a custom Style and use it for Button with Style element and Setter elementCreate a custom Style and use it for Button with Style element and Setter element
18.Style targeting a specific type of element(Button)Style targeting a specific type of element(Button)
19.Extending a Style using the BasedOn attributeExtending a Style using the BasedOn attribute
20.Target Type DerivativesTarget Type Derivatives
21.Use number substitutions for different culture settingsUse number substitutions for different culture settings
22.Implicit use of a StyleImplicit use of a Style
23.Create a Typed Style for ButtonCreate a Typed Style for Button
24.The dash style of a line is specified by the StrokeDashArray property that gets or sets a collection of double variablesThe dash style of a line is specified by the StrokeDashArray property that gets or sets a collection of double variables
25.Create a style for use as a FocusVisualStyle on a control.Create a style for use as a FocusVisualStyle on a control.
26.Style with Property Trigger.xamlStyle with Property Trigger.xaml