tags:

views:

373

answers:

4

Hi, I'm pretty new to WPF and i'm trying to load a XAML window and pass a variable to this XAML in its constructor or so, because i need it to load some items from this passed variable.

Could anyone point me to the direction of how to go about this please? How does one start up a XAML window and give it a variable please?

Thanks in advanced.. Erika

+1  A: 

Typically, in WPF, you'd create the items you want to load, and set the Window (or UserControl)'s DataContext to the class that contains your items. You can then bind directly to these in order to do custom display from the XAML.

Reed Copsey
I perhaps might not have been clear in my question (or i might be not understanding you! i'm very new to WPF), what im trying to do is pass an object to the XAML window so that the controls, which are already created and such will display the content passed from the objectIf thats what you meant, would it be possible to provide a rough example please? I would really appreciate thanks!
Erika
Is your background with Windows Forms? Other libraries in different languages? This will help point you in the right direction...
Reed Copsey
at the moment its all in WPF/XAML windows, all libraries are in C#
Erika
No - I meant, what did you know before trying to learn WPF?
Reed Copsey
C# and im not too bright at it, but i usually manage my way around
Erika
Well, MVVM works well for this - but it can be confusing at first, since WPF has a lot to learn. I wrote a lengthy series here: http://reedcopsey.com/series/windows-forms-to-mvvm/ Maybe it'll help.
Reed Copsey
My suggestion is basically to do (what is, at its core) MVVM. Use the data context, and bind to objects. Until you read up on it, it may be difficult to understand why this is better - the link above is a decent intro, IMO.
Reed Copsey
Thanks a lot Reed, thats a fantastic website and im currently looking into it trying to understand this MVVM business! One question however (probably proves im still trying to get to grasps with this new architecture), however, does this also apply to when i want to set something before/during loading of the XAML window? For example, I have a window, user clicks on something and then is displayed information according to what he has clicked. Sorry if im being a nuisance! and thanks a lot once again
Erika
*information is displayed on a new window for instance so i would need to know what had been clicked before
Erika
Erika: The "trick" is to start thinking in two terms - there's what the user sees (the window, etc), and what happens behind the scenes (the logic of your app). If you want a "user click" to change something logically, you'd use a command - which allows your ViewModel to change. The View will "change" to follow the ViewModel automatically because of data binding.
Reed Copsey
+2  A: 

Try to use MVVM (Model-View-ViewModel) pattern.

You need Model:

class Person
{
    public string Name { get; set; }
}

View is your window or UserControl.

ViewModel can be something like that:

class PersonViewModel : INotifyPropertyChanged
{
 private Person Model;
 public ViewModel(Person model)
 {
  this.Model = model;
 }

 public string Name
 {
  get { return Model.Name; }
  set
  {
   Model.Name = value;
   OnPropertyChanged("Name");
  }
 }

 public event PropertyChangedEventHandler PropertyChanged;
 private void OnPropertyChanged(string propertyName)
 {
  var e = new PropertyChangedEventArgs(propertyName);
  PropertyChangedEventHandler changed = PropertyChanged;
  if (changed != null) changed(this, e);
 }
}

Then you need to specify DataContext for your window:

View.DataContext = new PersonViewModel(somePerson);

And then define bindings in XAML:

<UserControl x:Class="SomeApp.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
<Grid>
    <TextBlock Text="{Binding Name}" />
<Grid>    
<UserControl>

MVVM makes code very elegant and easy.

You can also try PRISM or Caliburn (http://caliburn.codeplex.com/) frameworks but they are more complex.

darja
Hi, thanks for the excellent reply, im still trying it out and at the moment failing. I understood the binding part, however i think this is refering more to when i want to set the content inside a XAML window from the code of the XAML itself right? Or does this apply to when i want to set a Property to my XAML window during loading? In that case how should i call it to do the latter pls?
Erika
As I understood your question, you want to pass some data to WPF form. You can do it with initializing its DataContext. You don't need to write any special form constructor or other code. And binding mechanism will help you to display data and catch its changes.
darja
A: 

My issue was that i wanted to access a class outside of the XAML window, not communicate with the relative code via Binding. For this, all i needed to do was create a static class to hold the value i required. Simple, yet the solution escaped me at that point. Its a rather dirty way to go round solving the problem but it does the trick.

I would like to thank both contributors who helped me understand the MVVM architecture as I hadnt really understood that previously.

Thanks so much for the prompt reply and sorry if my question was easily missunderstood! Sometimes im not very good at conveying my ideas..

Erika
A: 

If you want to see a bit more on MVVM then check out this (MVVM is really powerful and a great way to develop testable code):

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

It should give you a very good idea of MVVM. There is an example you can download just under the heading where it says: Code download available from the MSDN Code Gallery.

Richard