How to declare an object in XAML : WPF

Posted by SunKwon Kim on 2008/10/12 12:58
Filed under WPF
Tags : , ,

Every object in XAML is declared in the resource


In the traditional application, we declare variables, objects, types freely. But when learning WPF, we have a problem where we can declare data. For examples, I want to declare double typed variable for data binding to fontsize of a button. Where we can do it? The answer is in a resource.


How to declare data type in XAML


Let's delcare double type in XAML. At first, we declare namespace System which contains double data type. In this examples, we define the System namespase named "system". Using the namespace, double data type named "doubleData" is declared in the window.resource. Notice the doubeData has initial value "20". If we don't set initial value, we will see error messages when compiling it.

<Window x:Class="DelcareObject.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <system:Double x:Key="doubleData">20</system:Double>
    </Window.Resources>

    <Grid>
        <TextBlock Text="Hi, My name is SunKwon Kim." FontSize="{StaticResource doubleData}"/>
    </Grid>
</Window>

How to declare object of a class in XAML


It is very similar with declaring data type. The difference is we need to define the namespace of the application instead of System and The class we want to declare is defined in the code. So when we want to make class and declare object of the class in XAML. Firstly, define the class in the code. Secondly, define application namespace in the XAML. Lastly, declare a object of the class in the resource.

Have a nice day.
2008/10/12 12:58 2008/10/12 12:58
Trackback URL >> http://crowsley2.cafe24.com/trackback/10
Leave a comment
[Log-ing][What is OpenID?]