Community
Showing results for 
Search instead for 
Do you mean 
Reply

Data binding WPF ListView with ACT! Contacts

Tuned Listener
Posts: 24
Country: USA

Data binding WPF ListView with ACT! Contacts

Published 26 April 07 12:13 PM

Now that WPF has been released I'm sure many ACT! SDK developers are wondering if it's possible to databind WPF with ACT! data.
In this entry I'll talk about how you can data bind a WPF ListView control with a ContactList from the ACT! SDK.

WPF data binding differs from WinForms data binding.  Some of the interfaces that are used in WinForms data binding don't work like you'd expect in WPF data binding.
For example, the ITypedList interface is what ACT! ContactList uses to bind the columns from Contacts to a grid control.  This interface doesn't work when data binding to the stock WPF ListView control which is the closest thing to a classic grid control that ships with WPF.

The WPF ListView control has different stock views you can apply to it and the one I'll be using is the GridView.

Here is the .xaml file of a window that shows a ListView control:

<Window
  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
   x:Class
="Act.UI.WPF.WPFEntityPickerWindow"
  
Title="Select Contacts" Height="300" Width="300">
   <
Grid>
      <
Grid.RowDefinitions>
         <
RowDefinition Height="0.886*"/>
         <
RowDefinition Height="0.114*"/>
      </
Grid.RowDefinitions>
      <
ListView x:Name="entityListView" IsSynchronizedWithCurrentItem="True">
         <
ListView.View>
            <
GridView>
           </
GridView>
         </
ListView.View>
      </
ListView>
      <
Grid RenderTransformOrigin="0,0" Margin="0,0,0,0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
      <
Grid.ColumnDefinitions>
         <
ColumnDefinition Width="0.5*"/>
         <
ColumnDefinition Width="0.5*"/>
     
</Grid.ColumnDefinitions>
      <
Button Grid.Column="0" Content="OK" Margin="2,2,2,2"/>
      <
Button Grid.Column="1" Content="Cancel" Margin="2,2,2,2"/>
      </
Grid>
   </
Grid>
</
Window>

The window is basically a simple contact picker form.  It shows list of contacts and has OK and cancel buttons.  The ListView is named entityListView.  Giving the xaml element a name allows us to reference it in the code behind file.

The first thing I need to do is get the contacts the I want to show in the picker form.

ContactList contacts = this.framework.Contacts.GetContacts(null);

contacts.FieldDescriptors.Add(this.framework.Contacts.GetContactFieldDescriptor("TBL_CONTACT.FULLNAME", true));

contacts.FieldDescriptors.Add(this.framework.Contacts.GetContactFieldDescriptor("TBL_CONTACT.COMPANYNAME", true));

Here I am getting a list of contacts from an already connected ActFramework object.  I'm specifying null as the sort criteria since for this sample I'm not concerned with the sort order.
I'm adding the Contact and Company fields to the ContactList's field descriptor collection so those fields will be what are shown in the grid.

GridView gridView = this.entityListView.View as GridView;
if (gridView != null)
{
     for(int i=0; i < contacts.FieldDescriptors.Count; i++)
     {
         
PropertyDescriptor field = contacts.FieldDescriptors[i] as PropertyDescriptor;

          if (field != null)
          {
              
GridViewColumn column = new GridViewColumn();
               column.Header = field.DisplayName;
               column.DisplayMemberBinding =
new Binding(CreateBindingPath(field));
               gridView.Columns.Add(column);
          }
     }
}

this.entityListView.ItemsSource = contacts;

Here I'm getting the GridView from the ListView control and am looping over all the fields in the fields collection of the ContactList and making a new column for each one.
Notice the data binding syntax for the columns occurs on the object level from the list not at the list level.  The DisplayMemberBinding property is set to a new Binding that uses a binding path that is relative to the Contact object.

The CreateBindingPath method is a helper method that I added to the class.

private string CreateBindingPath(PropertyDescriptor field)
{
    
return string.Format("Fields[{0}, true]", field.Name);
}

Here you can see the binding path syntax is a property off of the Contact object.  The Fields property on the Contact object is used to retrieve the data.  Notice when you use indexers in the path you don't use quotes on the strings, WPF figures out how to parse it by the commas.
The last line where we set the ItemSource property of the ListView is equivalent to setting the DataSource of a grid in WindowsForms.

This same sample will work with Groups, Companies or Contacts since they all derive from MutableEntity.

Copper Contributor
Posts: 119
Country: USA

Re: Data binding WPF ListView with ACT! Contacts

Scanman said on June 15, 2007 03:32 AM:

Presume the WPF is a part of the 3.0 Framework, will this work for 2.0 as i assume that ACT! still uses 2.0 framework
Tuned Listener
Posts: 24
Country: USA

Re: Data binding WPF ListView with ACT! Contacts

Mark said on June 15, 2007 11:54 AM:

Good question.

ACT! is built using .net 2.0, however in .net 3.0 the CLR (common language runtime) didn't change versions, it's still 2.0.

3.0 just added more dlls to the framework, specifically WPF, WCF and WF.

So you'll need .net 3.0 to be able to build WPF applications but they will work fine with .net 2.0 assemblies like the ACT! SDK.
Highlighted
Copper Contributor
Posts: 61
Country: USA

Re: Data binding WPF ListView with ACT! Contacts

Pierre said on June 18, 2007 01:18 AM:

That is correct. ACT! ships and uses .net 2.0, but the technique that Mark describes is about connecting to the ACT! SDK from an external application. Obviously, if one was to use WPF, then .net 3.0 is required. Note that the CLR has not changed AT ALL between 2.0 and 3.0 (only extensions to the framework were added by Microsoft), so ACT! will work fine with .net 3.0 This link shows the version numbers of .net over the different releases and what you get with 3.5, a list of the new DLLs. http://www.danielmoth.com/Blog/2007/06/net-framework-35.html CLR version numbers: http://www.danielmoth.com/Blog/2007/05/clr-v20-remains-at-same-version.html