views:

48

answers:

2

Hi,

i have generated a Linq to Sql class which looks like this.

alt text

so i have 3 querys which gets my data.

private IQueryable<Gesellschaft> loadedGesellschaft;
private IQueryable<Anschrift> loadedGesellschaftAnschrift;
private IQueryable<Email> loadedGesellschaftEmail;
private lgDataContext completeGesellschaft;

private void Button_Click(object sender, RoutedEventArgs e)
    {
        completeGesellschaft = new lgDatacontext();
        loadedGesellschaft = completeGesellschaft.Gesellschaft.Where(gid => gid.GID == 2);
        loadedGesellschaftAnschrift = completeGesellschaft.Anschrift.Where(FK_GID => FK_GID.FK_GesellschaftId == loadedGesellschaft.First().GID);
        loadedGesellschaftEmail = completeGesellschaft.Email.Where(FK_GID => FK_GID.FK_AnschriftId == loadedHauptanschrift.First().idAnschrift);
    }

After this i want to put these 3 on my page. The Result is something like this there one Office(loadedGesellschaft) and that has maybe more than one Adress(loadedGesellschaftAnschrift) and has maybe more than one Email(loadedGesellschaftEmail) so i have on my window some textboxes which contain the fields from loadedGesellschaft and Adresses and Emails are stored in comboboxes.

do i always have to bind the itemsource of one Control e.g.

<ComboBox Name="CBox_GDEmail" />

CBoxGDEmail.Itemsource = loadedGesellschaftEmail;

or is there an possibility to put all three objects together to the datacontext of the window ?

+1  A: 

Combine your three objects together in a ViewModel object.

Robert Harvey
+1  A: 

First, create three classes: Gesellschaft, Anschrift, and Email. These classes are view models; they expose any property whose value you want to see in the view. Make Gesellschaft expose an Anschriften property of type IEnumerable<Anschrift>, and Anschrift expose an Emails property of type IEnumerable<Email>. (I'm just sort of guessing at what the plural of Anschrift is; pretty much all of my knowledge of German comes from board games.)

In your XAML, create three DataTemplates, e.g.:

<DataTemplate DataType="{x:Type local:Gesellschaft}">
   <WrapPanel>
      <Label>Name</Label>
      <TextBlock Text="{Binding Gesellschaftname}"/>
      ...
      <ListBox ItemsSource="{Binding Anschriften}"/>
  </WrapPanel>
</DataTemplate>

Obviously you'll want to use a saner layout than sticking a bunch of controls in a WrapPanel; this is just a proof of concept. The DataTemplate for Anschrift should similarly have a ListBox whose ItemsSource is bound to Emails.

Once you've done this, all you need in your XAML is to set the DataContext of a ContentPresenter to an instance of Gesellschaft. It will be rendered using the DataTemplate that you've defined for that type. Its ListBox will contain an item for each Anschrift, rendered using that type's template. U.s.w.

Congratulations, you're now using the MVVM pattern just like all the cool kids. There's a lot more to learn about than just this, but this is a good start.

Robert Rossney
i`m trying to transform this now to my needs. A Question im trying to add INotifyPropertyChanged to my new ViewModel Class should i take instead of IEnumerable<Email> e.g. an ObservableCollection<Email> ? (Anschriften is the right plural of Anschrift ;))
Mark
Yes, if you're going to be adding or removing entries from the collection, you'll want it to be an ObservableCollection<Email>.
Robert Rossney
Hi another time, how do you implement the VW if it has settings, umm like i have a datagrid and i want to specify the columns the it would display. Say i want to specify which columns are displayed from the database in the grid. So im a little bit stuck in that. My VM Class should then consist of lists of all columns which are possible ? And how do i tell my WPF apllication to only Display that Columns which i want to be displayed. I hope that i announced that probably.
Mark
There are many different ways of handling this, and the appropriate one really depends on your specific requirements. I think you should probably post this as a new question.
Robert Rossney