views:

38

answers:

2

I have an abstract object called Applicant and two different types of objects that inherit from Applicant called Business and Individual. So I have three classes that look like this:

public abstract class Applicant
{
...
}

public class Individual : Applicant
{
  ...
}

public class Business : Applicant
{
  ...
}

Now in the DataGrid I want to show all the details of Applicant object. When you choose a row I want to show details of either the business or individual as a internal grid. Something like this

<DataGrid>
     <DataGrid.Columns>
          <!--Show different columns -->
     </DataGrid.Columns>
     <DataGrid.RowDetailsTemplate>
         <!--Show if Individual -->
         <DataGrid>
              <DataGrid.Columns>
                <DataGridTextColumn Header="First Name" ... />
                <DataGridTextColumn Header="Last Name" ... />
              </DataGrid.Columns>
          </DataGrid>
          <!--Show if business -->
          <DataGrid>
              <DataGrid.Columns>
                <DataGridTextColumn Header="Business Name" ... />
                <DataGridTextColumn Header="Tax id" ... />
              </DataGrid.Columns>
          </DataGrid>
     </DataGrid.RowDetailsTemplate>
</DataGrid>

Now I'm not sure if I need to use a Triggers or Behaviors to accomplish this? Thanks for everyones help! FYI I'm using Silverlight 4.0 with Prism.

A: 

I'd user use neither:

  • Have two views (grids) - one renders Individual details, another renders Business details.
  • Define a ContentControl inside of RowDetailsTemplate and make it a region.
  • Then handle SelectedApplicant change in your ViewModel for the main grid so that it would activate the proper view in the region depending on the type of selected applicant.
PL
That sounds like a good idea. Where would I put SelectedApplicantevent because right now I have a collection of applicants like this ObservableCollection<Applicant>? Maybe I could even use LoadingRowDetails instead?
Michael
If you are in viewmodel world, then you wouldn't have an event - you'd bind SelectedItem of the grid to CurrentApplication property in your viewmodel. Thus when selection changed set method on the property would be automatically called.
PL
I added this:<sdk:DataGrid.RowDetailsTemplate><DataTemplate><ContentControl regions:RegionManager.RegionName="myRegion" /></DataTemplate></sdk:DataGrid.RowDetailsTemplate>and I'm listening on event SelectionChanged and callingvar region = _regionManager.Regions["SearchesContainerRegion"];but get exception"The region manager does not contain the myRegion region."
Michael
A: 

I don't think you need neither behaviours nor triggers but databinding. There is a great simple intro what databinding is in msdn documentation.

The thing that is unique in your cituation is that you don't have one but two different DataTemplates for your datagrid and you need to change them on the fly. I found a blogpost and a silverlight.net forum thread about that subject: Changing Data-Templates at run-time from the VM and forum.

First one uses some pretty simple codebehind logic and the one in the forum does the same thing using an IValueConverter.

texmex5