Thursday, June 9, 2016

Understanding Resources in WPF


In WPF Resources are use to define object in XAML to reuse in Application.
Resources are not the part of visual three, but can be used in XAML to provide value to the XAML property attributes. Resources can be create and access in XAML and Code in both side viceversa.
E.g.  Suppose need an object of solidcolorbrush for background of some buttons with same color.
One option we can create it in XAML

<Window x:Class="WPFResources.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:clrType="clr-namespace:System;assembly=mscorlib">
    <Window.Resources>
        <SolidColorBrush x:Key="myColorBrush" Color="Red"/>
    </Window.Resources>
</Window>

This resource is created under window resource dictionary
Now any button (any ui control) can use this resource in this window

<Window x:Class="WPFResources.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:clrType="clr-namespace:System;assembly=mscorlib">
    <Window.Resources>
        <SolidColorBrush x:Key="myColorBrush" Color="Red"/>
    </Window.Resources>
    <Grid>
        <Button Background="{StaticResource myColorBrush }"/>
        <Ellipse Fill="{StaticResource myColorBrush }"/>
        <Grid Background="{StaticResource myColorBrush }"></Grid>
    </Grid>
</Window>

Resource can be declared in
  1.      App.XAML
  2.       Resource Property of the current window
  3.       Resources property of any FrameworkElement or FrameworkContentElement
  4.       Separate XAML File 
  • As global application scope within the application App.xaml file
  • As Window level scope within the Resources property of the current Window
  • Within the Resources property of any FrameworkElement or FrameworkContentElement.
  • Separate XAML resource file.
In App Level in App.XAML file

<Application.Resources>
        <SolidColorBrush x:Key="MyColorBrush" Color="Red"></SolidColorBrush> 
    </Application.Resources>

Window Level

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="MyColorBrush" Color="Red"></SolidColorBrush>
    </Window.Resources>  
</Window>

Stack Level

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:clrType="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="MyColorBrush" Color="Red"></SolidColorBrush>
    </Window.Resources>
    <StackPanel>
        <StackPanel.Resources>
            <clrType:String x:Key="strText">Hello</clrType:String>
        </StackPanel.Resources>
        <TextBox Text="{StaticResource strText}"></TextBox>
    </StackPanel>

</Window>


No comments:

Post a Comment