views:

31

answers:

1

I have 3 data tables:

User

UserID, UserName
1, Bat
2, John
etc...

Member

MemberID, Member
1, Local
2, Zone
etc...

UserMember

UserID,MemberID
1, 1
1, 2
2, 1
etc...

On the user input XAML Form in WPF
Check list box is bound to Member table.

My Question:

I want it so that when the user checks member type, the selected values automatically get inserted into UserMember table. And when the the user unchecks member type, the selected values are deleted from UserMember table.

How can I do this in WPF?

+1  A: 

It depends on what kind of software methodology you want to use (I like MVVM with WPF) what kind of ORM(Linq, EF, Castle, etc.).

your could create a ViewModel like so:

public class UserViewModel
{
  int UserId {get;set;}
  Collection<Member> MemberList {get;set;}
}

public class Member
{
  bool _IsMember;
  bool IsMember 
  {get return _IsMember;set _IsMember = value; EditMemberStatus();}
  int MemberId {get;set;}
  string Member {get;set;}

  void EditMemberStatus()
  {
    if (IsMember)
      //Code to add row into db using your ORM choice
    else
      //Code to remove row from db
  }
}

in your xaml you could do this:

<ListView ItemsSource={Binding MemberList}>
  <ListView.View>
    <GridView>
      <GridViewColumn Header="IsMember"}>
        <CheckBox IsChecked="{Binding IsMember}"/>
      </GridViewColumn
      <GridViewColumn Header=Member DisplayMemberBinding={Binding Member}/>
    </GridView>
   </ListView.View>
</ListView>

And finally in the code behind of your xaml you have this

public class UserView
{
  UserView()
  {
     InitializeComponent(); 
     DataContext= new UserViewModel();
  }
}

This is just a skeleton, hope this helps. It's also only one way of a myriad of ways to do it.

Jose