Getting Started with WPF & Silverlight

esps
esps used Ask the Experts™
on
Hi Experts,
I have a number of years experience developing Windows Forms apps using Visual Basic.
I am now starting to explore WPF and Silverlight.
Please point me to some reading material to get started and a few examples of the real basic stuff that I am struggling with like:
How to display text in a text box from a variable. The variable gets its value in a codebehind routine. Value is ther but textbox stays blank.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
Some of the books to start with Silverlight are:
1. Pro Silverlight 3 In VB.net
2. Silverlight  4 in Action
3. Essential Silverlight 3 - Ashraf Michail (kinda advanced)

Please post some code your code.

Arun

Author

Commented:
hi nmarun
Using the following codebehind snippet as an example:
 Dim myrow As System.Data.DataRow
        Dim dbasevariable As String = 0
        Do While True
            If myrow("Description") = "xxx" Then
                dbasevariable = 1
                Exit Do
            Else
                dbasevariable = 2
                Exit Do
            End If
        Loop
        TextBox1.Text = dbasevariable.ToString
What must I do or add to the XAML to get Textbox1 to actually display its value?
In this case the XAML is:
 <TextBox Height="23" HorizontalAlignment="Left" Margin="252,173,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Top Expert 2009

Commented:
Try to set a value for the Text property in the xaml itself and see if it is getting displayed. There's a possibility that this control is getting hidden behind something else or is getting shifted outside the bounds of your layout control.

<TextBox Height="23" HorizontalAlignment="Left"  Name="TextBox1" VerticalAlignment="Top" Width="120"  Text="I see this" />

Arun

Author

Commented:
That does work but how do I use the variable in XAML?
Top Expert 2009

Commented:
There was a reason I asked you to use the hard-coded value. Please see that I've also removed the Margin attribute on the TextBox. Do try without the Margin attribute and use the code that you have and see if it works. Something tells me that these margin settings are causing the textbox to be placed in a location outside the layout.

Arun

Author

Commented:
Same result.
The "I see this" is diplayed in the textbox (upper left corner) the moment I run the app.
With a breakpoint set on the codebehind ad stepping through, the new value is assigned to the textbox correctly but the value simply does not replace the hardcoded one
Top Expert 2009

Commented:
Please upload your solution to http://www.ee-stuff.com/ and give me the link. I'll have a look at it.

Arun

Author

Commented:
Hi nmarun,
Loading the solution won't help much since I am making use of 3rd party librarires that is licensed. Like Aspose.
I have attached the full section of the codebehind as well as the XAML
hope this will help
Otherwise I will load the entire solution.
Let me know
regards
Imports AngloDoc.DocumentCrypto
Imports System.IO
Imports Aspose.Words
Imports System.Data
Imports System.Windows.Threading
Imports System.Threading





Public Class Criteria
    Public Sub DoEvents()

        Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
        New ThreadStart(AddressOf doNothing))
    End Sub

    Public Sub doNothing()

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Textboxvalue = "tt"
        TextBox4.Text = Textboxvalue

        Dim wordLicense As New Aspose.Words.License()
        wordLicense.SetLicense("Aspose.Total.lic")

        Dim myConnection As SqlClient.SqlConnection
        Dim MyCommand As New SqlClient.SqlCommand()
        Dim myDA As New SqlClient.SqlDataAdapter()
        Dim myDS As New SceDMSDataSet()
        'Dim cmd As SqlClient.SqlCommand = Nothing
        Dim aa As String
        Dim builder As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(myDA)
        builder.QuotePrefix = "["
        builder.QuoteSuffix = "]"
        Dim sql As String

        ' Try
        myConnection = New SqlClient.SqlConnection(My.Settings.SceDMSConnectionString)
        MyCommand.Connection = myConnection
        sql = "SELECT  * from Documents_Master " ' PublishFileAndPathName "

        MyCommand.CommandText = sql

        MyCommand.CommandType = CommandType.Text
        myDA.SelectCommand = MyCommand
        aa = MyCommand.CommandText
        myConnection.Open()

        myDA.Fill(myDS, aa)
        Dim myrow As DataRow
        Dim converdocumentName As String = ""
        For Each myrow In myDS.Tables(sql).Rows
            If IsDBNull(myrow("PublishFileAndPathName")) = False Then
                converdocumentName = myrow("PublishFileAndPathName")
                Converdoc(converdocumentName)
            End If
            converdocumentName = myrow("FileAndPathName")
            ConverWorking((converdocumentName))
            var_ID = myrow("ID").ToString   'var_ID is a global variable set in module1
            TextBox4.Text = var_ID 'setting to textbox4 just for test actually want to set elsewhere in code
        Next


<Window x:Class="Criteria"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Criteria (Batch Run)" Height="363" Width="655" Topmost="True">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="271,94,0,0" Name="TextBox1" VerticalAlignment="Top" Width="322" />
        <Button Content="Perform Batch Run" Height="23" HorizontalAlignment="Left" Margin="480,246,0,0" Name="Button1" VerticalAlignment="Top" Width="113" />
        <Image Height="39" HorizontalAlignment="Left" Margin="446,12,0,0" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="175" Source="/AngloDoc;component/Images/logo_aa.gif">
            <Image.OpacityMask>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="White" Offset="1" />
                </LinearGradientBrush>
            </Image.OpacityMask>
        </Image>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="271,132,0,0" Name="TextBox2" VerticalAlignment="Top" Width="322" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="271,174,0,0" Name="TextBox3" VerticalAlignment="Top" Width="322" />
        <Label Content="Replacement Graphic (full path)" Height="28" HorizontalAlignment="Left" Margin="56,89,0,0" Name="Label1" VerticalAlignment="Top" Width="191" />
        <Label Content="Text to Replace (1st Instance)" Height="28" HorizontalAlignment="Left" Margin="56,127,0,0" Name="Label2" VerticalAlignment="Top" Width="182" />
        <Label Content="Text to Replace (2nd Instance)" Height="28" HorizontalAlignment="Left" Margin="56,169,0,0" Name="Label3" VerticalAlignment="Top" Width="170" />
        <Label Content="Provide Details to be Replaced for Batch Run" Height="28" HorizontalAlignment="Left" Margin="119,23,0,0" Name="Label4" VerticalAlignment="Top" Width="313" Foreground="#FF000019" FontStyle="Normal" FontWeight="Black" FontSize="14" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="190,226,0,0" Name="Button2" VerticalAlignment="Top" Width="75" Visibility="Hidden" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="276,275,0,0" Name="TextBox4" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>

Open in new window

My toes just curled right up into my shoes when I saw this code. I sure hope this is purely to learn concepts, and not how you intend to architect, mashing all this logic into the UI.

You need to change how you think in WPF. You don't stuff data into controls like we did in the goold old days. Everything's done through binding. I see no binding in your code.

Search on "wpf binding" and you'll find countless examples.

In a nutshell, you want to tell the textbox to bind to a property of the datacontext. The datacontext is the object holding the data. The controls then bind to properties of that object. A DC can be assigned at the page level, or at sub-containers. It's inherited, so assigning it a level makes it the default for nested containers.

Now, when the underlying data changes, the UI automatically displays it, via change notification. This is built in, but if you're binding to custom properties you need to implement the notification yourself (simply by implementing INotifyProperyChanged  or by using dependency properties).
Top Expert 2009

Commented:
ToddBeaulieu - the title of this thread says - 'Getting Started...', so this seems like a learning session (if I'm wrong I totally agree with you)

esps - I see you are using a Grid, but you don't have rows and columns defined. Instead of using Margins, try defining <Grid.Rows> and <Grid.Columns> with their respective heights and widths. Then you can place each of your control in a specific row - column location without needing Margins.

If this does not help, do a screencapture of the UI at runtime and upload it.

Arun

Author

Commented:
Hi ToddBeaulieu,
Yes, I am indeed an 'old school programmer' and please note that this is my very first attempt at WPF.
I need to get this 'little' conversion program out and I decided to try WPF.
I therefore obviously try to apply what I know into a new environment and in the process getting horibly stuck with even the most elementary code (or supposed to)
Datacontext and data binding is foreign to me at this stage and would appreciate specific, simple examples. I am more than willing to learn and study properly in the medium to long term but need to get past this first, simple hurdle.
Arun, there is no grid. The grid reference in the XAML must have been inserted by VS 2010 somehow.
There are labels, textboxes and a button on the form
Thanks for your help.
Top Expert 2009
Commented:
Please replace the grid control and with a canvas or you can think of structuring your UI using a grid (recommended).

Arun
The grid's not the issue. In WPF the grid is used as a layout tool and is unrelated to a data grid. You'd use a canvas if you needed to precisely and manually place controls, which 99% of the time is not what you want.

You still need to implement binding here if you want the UI to display data. Have you looked into this yet?

Author

Commented:
Thank you for trying to help me.
Decided to explore silverlight and asp for web 'stuff' and stick to Windows forms a little bit longer for the the 'real' business apps.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial