Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
595
DataBinding to WCF
posted

My situation is that I have a user control inside the group header template of the outlook bar.  This user control has a bunch of controls that it manages as well.  The outlook bar is bounded to a wcf service and passes a value to the user control, the user control then uses that value and gets it's own values from the wcf service.  The issue I'm having is which ever group header I select first all the other group content is the same as that one. Here the test I used to get this issue:

WCF Service:

[OperationContract]
public List<GroupHeader> GetGroups()
{
 List<GroupHeader> groups = new List<GroupHeader>();
 for (int i = 0; i < 10; i++)
 {
  groups.Add(new GroupHeader() { ID = i, Name = "Group " + i.ToString() });
 }

 return groups;
}
[OperationContract]
public List<GroupItem> GetItems(int groupID)
{
 List<GroupItem> items = new List<GroupItem>();

 for (int i = 0; i < 10; i++)
 {
  if (i == groupID)
  {
   for (int j = 0; j < 15; j++)
   {
    items.Add(new GroupItem() { ID = j, Name = "Group " + i.ToString() + " - Item " + j.ToString() });
   }
   break;
  }
 }

 return items;
}


[DataContract]
public class GroupHeader
{
 [DataMember]
 public int ID { get; set; }
 [DataMember]
 public string Name { get; set; }
}

[DataContract]
public class GroupItem
{
 [DataMember]
 public int ID { get; set; }
 [DataMember]
 public string Name { get; set; }
}

Main Outlook bar Control Xaml:


<igOB:XamWebOutlookBar x:Name="MainOutlookBar" Width="300" Height="500">
 <igOB:XamWebOutlookBar.GroupHeaderTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding Path=Name}"></TextBlock>
  </DataTemplate>
 </igOB:XamWebOutlookBar.GroupHeaderTemplate>
 <igOB:XamWebOutlookBar.GroupContentTemplate>
  <DataTemplate>
   <medscc:GroupItem GroupID="{Binding Path=ID}" />
  </DataTemplate>
 </igOB:XamWebOutlookBar.GroupContentTemplate>
</igOB:XamWebOutlookBar>

Main Outlook bar Control Code Behind:

public MainOutlookBar()
{
 ServiceClient.GetGroupsCompleted += new EventHandler<GetGroupsCompletedEventArgs>(ServiceClient_GetGroupsCompleted);
 InitializeComponent();

 ServiceClient.GetGroupsAsync();
}

void ServiceClient_GetGroupsCompleted(object sender, GetGroupsCompletedEventArgs e)
{
 if (e.Error == null)
 {
  MainOutlookBar.GroupsSource = e.Result;
 }
}

User Control Xaml:

 <ListBox x:Name="GroupItems">
 <ListBox.ItemTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding Name}"></TextBlock>
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

User control Code behind:

#region Private Members
private ServiceClient ServiceClient;
#endregion

public static readonly DependencyProperty GroupProperty =
 DependencyProperty.Register("GroupID",
 typeof(int),
 typeof(GroupItem),
 new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));

public int GroupID
{
 get { return (int)GetValue(GroupProperty); }
 set
 {
  SetValue(GroupProperty, value);
  ServiceClientt = new ServiceClient();
  ServiceClient.GetItemsCompleted += new EventHandler<GetItemsCompletedEventArgs>(ServiceClient_GetItemsCompleted);
  ServiceClient.GetItemsAsync(value);
 }
}

public GroupItem()
{
 
 InitializeComponent();
}

void ServiceClient_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
{
 if (e.Error == null)
  GroupItems.ItemsSource = e.Result;
}

public static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
 GroupItem groupItem = (GroupItem)d;
 groupItem.GroupID = (int)e.NewValue;
}

Parents
No Data
Reply
  • 3071
    Verified Answer
    posted

    Hi MrYang,
    The problem is in GroupID property in GroupItem UserControl
           public static readonly DependencyProperty GroupProperty =
                                         DependencyProperty.Register("GroupID", ...
    It is better to use GroupIDProperty name;
    and
    You don’t need this line: groupItem.GroupID = (int)e.NewValue;
    - it breaks Binding and GroupID stays unchanged;
    You can move lines from GroupID property setter to the OnGroupIDPropertyChanged handler.
    Regards,
    Marin

Children