Learning WPF - Needs Some Basics

J

jehugaleahsa

Hello:

I have been trying to learn the basics about WPF. I am comfortable
with binding to other control properties and binding to XML, but I
have been having a lot of trouble binding to data objects (which is
really all I care about). I need some pointers. I thought if someone
could show me an example it would help me.

The example I would like to see:

I would like to have a form that has a collection of Person. Person
would be a class with the properties of Name and Age. There can be
multiple people in the collection. How I populate the collection is
irrelevant at this point (I'll probably just hard-code it).

I would like the form to provide a text box and a button for entering
in, say an Age. The button click event would populate the collection
with people under the given age.

I would to display all the people in a grid. I would also like there
to be two text boxes below that held the name and age of the currently
selected person. When you select a different person from the grid,
that person's information would show up below in the text boxes. When
you change a value in the text boxes below, it would update the grid's
data.

That's the simple example I want. In Windows Forms it was easy to do
this. You simply created a BindingSource and set its DataSource
property to a BindingList<Person>. You populated the BindingList in
order for the Form to show the people. You had Person implement
INotifyPropertyChanged. I am assuming it is just as easy in WPF. I
simply haven't found an example that really lays it out. Maybe I have
been looking in the wrong place.

I have been watching MS' webcasts and they have been illuminating. I
have been visiting tons of web sites. I have been finding good
tutorials and examples, but none that work with data objects in the
way I am looking for.

I made a lot of Windows Forms before I truly understood what I was
doing. I wasted a lot of time making forms that, while functional,
were not as versatile as the forms I make now. I had a huge learning
curve and when I was to the point I am now, the work I did beforehand
was less than satisfactory. The old forms required a lot of rework. I
would like to avoid this rework in WPF. I want to learn the standard
practices up-front.

Any links to good resources would be greatly appreciated. I am willing
to do the work to get on top of things. Mostly, I would like to see
the suggested example above implemented. It is basic enough that it
shouldn't be difficult to implement, but complex enough to give me the
general idea. I know it would require a lot of work, but I'd be super
appreciative.

Thanks,
Travis
 
M

Mr. Arnold

Any links to good resources would be greatly appreciated. I am willing
to do the work to get on top of things. Mostly, I would like to see
the suggested example above implemented. It is basic enough that it
shouldn't be difficult to implement, but complex enough to give me the
general idea. I know it would require a lot of work, but I'd be super
appreciative.

Free ebooks man free ebooks are all over the place as 'cloud' technology
providers', like Google and others are paying publishers to put the books
on the Internet. You can read the whole book on line or you can download
them.

<
http://www.google.com/search?hl=en&...ok+on+windows+presentation+foundation&spell=1>

It's the only way to fly if the book is out there on the Internet and it's
free.
 
J

jehugaleahsa

Free ebooks man free ebooks are all over the place as 'cloud' technology
providers', like Google and others  are paying publishers to put the books
on the Internet. You can read the whole book on line or you can download
them.

<http://www.google.com/search?hl=en&ei=EWTrSZ7QGMqrtgfk2bXUBQ&sa=X&oi=...>

It's the only way to fly if the book is out there on the Internet and it's
free.

I bought a book from Wrox a few days ago. It didn't take me long to
realize that it was written for designers and not so much for
developers. It would start to get into a topic and then overlook the
details. I learned more from watching a stream from Microsoft in 20
minutes than I did from reading 4 chapters from this book. Of course
publishers are going to market a book toward the designer; it gives
them a wider audience. I was a little frustrated. Perhaps I should
have examined what's out there before I spent any money.

I have found some more examples online. I found one that almost
perfectly does what I want. I am not a fan of copying other's work off
the Internet. You don't learn much that way, except how to hack and
slash. I am trying to learn the theory behind WPF - the stuff you
really need to know if you're going to using it on the large scale. I
did manage to hack and slash the following code:

<!-- Main.xaml -->
<Window x:Class="TestWPF.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TestWPF="clr-namespace:TestWPF"
xmlns:ComponentModel="clr-
namespace:System.ComponentModel;assembly=WindowsBase"
Title="Main"
Height="Auto"
Width="Auto">

<Window.Resources>
<ObjectDataProvider x:Key="People"
ObjectType="{x:Type TestWPF:people}"/>
</Window.Resources>

<Grid>
<StackPanel>
<ListView Name="_peopleListView" Margin="0,0,0,50"
DataContext="{StaticResource People}" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name"
DisplayMemberBinding="{Binding Path=Name, Mode=TwoWay}"/>
<GridViewColumn Header="Age"
DisplayMemberBinding="{Binding Path=Age, Mode=TwoWay}"/>
</GridView>
</ListView.View>
</ListView>
<StackPanel Orientation="Horizontal">
<Label Content="Name:"/>
<TextBox Name="_nameTextBox"
Width="250"
VerticalAlignment="Center">
<TextBox.Text>
<Binding ElementName="_peopleListView"
Path="SelectedItem.Name"
Mode="TwoWay"

UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>
<Label Content="Age:" />
<TextBox Name="_ageTextBox"
Width="250"
VerticalAlignment="Center">
<TextBox.Text>
<Binding ElementName="_peopleListView"
Path="SelectedItem.Age"
Mode="TwoWay"

UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>
</StackPanel>
</StackPanel>
</Grid>
</Window>

// Main.xaml.cs
using System.Windows;
using System.Windows.Controls.Primitives;
using TestWPF;
using System.Windows.Data;

namespace TestWPF
{
/// <summary>
/// Interaction logic for ObjectBinding.xaml
/// </summary>

public partial class Main : Window
{
public Main()
{
InitializeComponent();
ObjectDataProvider provider = (ObjectDataProvider)Resources
["People"];
People people = (People)provider.Data;
people.Add(new Person("Bob"));
people.Add(new Person("Tammy"));
people.Add(new Person("Reginald"));
}

}
}

// People.cs
using System;
using System.Collections.ObjectModel;

namespace TestWPF
{
public class People : ObservableCollection<Person>
{
}
}

// Person.cs
using System.Collections.Generic;
using System.ComponentModel;

namespace TestWPF
{
public class Person : INotifyPropertyChanged
{
private string _name;
private int _age;
private event PropertyChangedEventHandler _propertyChanged;

public Person(string name)
{
Name = name;
}

public string Name
{
get { return _name; }
set { _name = value; propertyChanged("Name"); }
}

public int Age
{
get { return _age; }
set { _age = value; propertyChanged("Age"); }
}

private void propertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs
(propertyName));
}

protected virtual void OnPropertyChanged
(PropertyChangedEventArgs e)
{
if (_propertyChanged != null)
{
_propertyChanged(this, e);
}
}

event PropertyChangedEventHandler
INotifyPropertyChanged.PropertyChanged
{
add { _propertyChanged += value; }
remove { _propertyChanged -= value; }
}
}
}

Now I just wish I knew how to use a private readonly People that is
declared in my code-behind. I hate putting it in Resources. Any ideas?
 
M

Mr. Arnold

I bought a book from Wrox a few days ago. It didn't take me long to
realize that it was written for designers and not so much for
developers. It would start to get into a topic and then overlook the
details. I learned more from watching a stream from Microsoft in 20
minutes than I did from reading 4 chapters from this book. Of course
publishers are going to market a book toward the designer; it gives
them a wider audience. I was a little frustrated. Perhaps I should
have examined what's out there before I spent any money.

I have found some more examples online. I found one that almost
perfectly does what I want. I am not a fan of copying other's work off
the Internet. You don't learn much that way, except how to hack and
slash. I am trying to learn the theory behind WPF - the stuff you
really need to know if you're going to using it on the large scale. I
did manage to hack and slash the following code:


Now, that's funny. You had better learn to hack, slash and not reinvent
the wheel in the real world. You learn what it's doing on the hack and
slash. :) There is nothing wrong with it
Now I just wish I knew how to use a private readonly People that is
declared in my code-behind. I hate putting it in Resources. Any ideas?

http://en.csharp-online.net/const,_static_and_readonly

Or you could get rid of the "set" in the property to make it a read only
object too I guess.
 
J

jehugaleahsa

Now, that's funny. You had better learn to hack, slash and not reinvent
the wheel in the real world. You learn what it's doing on the hack and
slash. :) There is nothing wrong with it

I agree it is sometimes nice to see how it is done. You couldn't do
your day-to-day work without it. I am talking about those so-called
programmers who do nothing but string together examples off of web
sites and book examples. When you start doing that, you _stop_
learning the code and start hacking and slashing. I think it is more
important to understand _why_ something works and then, while not
reinventing the wheel, write the code that works specific to your
situation. I can always tell the difference between code that was
written with understanding and code that was slapped together until it
works. It's not like your writing your own List class or GridView user
control.

Most examples I find on the Internet involving ADO .NET examples
completely lack any resource management, aka. using statements. That
kind of code doesn't scale well and may only suite scripters. But
serious developers writing multi-thousand line projects with strict
runtime constraints can't let a connection go undisposed. Standardized
code formatting, good commenting and avoiding unnecessary code are all
part of it. That is something you'll be hard-pressed to find online
for free. Most examples are written to be concise. People shouldn't
take that concise code for gold.

I saw an web cast on WindowsClient yesterday where this dude was using
a DispatchTimer and PNGs to animate an explosion in WPF. I almost
cried when he wrote this code:

string url = "pack://application:,,,/explosion ";
if (count < 10) url = url + "0";
url = url + count;
url = url + ".png";

While this code is probably more efficient, requires less knowledge of
string formatting and is perfectly correct, it is a lot more complex,
and therefore harder to maintain, than this:

string url = String.Format("pack://application:,,,/explosion
{0:00}.png", count);

The presenter's code example was probably made to be more easy to
understand. I'm not knocking him or anything. The point is that this
code probably was copied and pasted 1000 times by other programmers on
the net, completely unaware of the fact that a single line of code
could have achieved the same thing. So, I'm not saying to never use
the Internet for inspiration, just don't take every example as the
_only_ way.
http://en.csharp-online.net/const,_static_and_readonly

Or you could get rid of the "set" in the property to make it a read only
object too I guess.- Hide quoted text -

- Show quoted text -

I did find an example where someone placed a public getter in a
Window's code-behind. Making a public property on a Window gives me
the chills.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top