views:

214

answers:

3

Hi,

I have the following scenario:

I have one window say MainWindow where I am displaying the list of activities as per the specific user from the database. There is a one button present on the window. By clicking on that button a new window is getting opened having all the list of activities from the master table. Now I want add a chechbox on the second window against each item dynamically so that user can select/deselect the activities. Those selected/deselected values should save in the database and Parent/MainWindow should refreshed after clicking on the done button and changes should reflect in the MianWindow. But I am not getting how to dynamically creating the checkboxes against each list item and binding with the xaml and select/deselect the checkbox.

Kindly suggest with samples or examples.

Thanks

+1  A: 
Veer
Thanks for the reply. I have done that. Values are coming alongwith the checkboxes. Values are coming from database and checkboxes are dynamically created. Now I want to preselect the checkboxes as per the table values. How to do that. I am returning the list.Thanks
Tarun
@Tarun: You can bind the values to the checkbox using a converter-http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter.aspx. Send me a sample of List data.
Veer
Hi Veer,I have one MainWindow, where am showing List of activities asp per the specific user. There is a button to add new activities in the existing list. By clicking Add NewActivities button a new window is getting opened with the whole list and with the specific user activities with preselect checkboxes. I want to preselect the values as per the database values. MainWindow:JoggingRunningAdd new Activities window:Jogging checkboxRunning checkboxNappingKindly Suggest?
Tarun
@Tarun: If Activity is a ViewModel and not a Model, then you can better add a property called isPresent. Else you should create a ViewModel that would act as a mirror for your model for other properties and also includes this extra property isPresent so that it could be bound to the checkboxes. Tell me if activity is a Model or a wrapper. In either case, show some code of your Activity class so that I would modify it and you may get the picture better. Also tell me more about the new Window. Does it have an OK/Cancel button to save changes? or data is saved on clicking the checkboxes at once?
Veer
A: 

@Veer.Activities is a Model where I have defined all the properties Like activityname,checkboxes. Here is the code:Activities.cs

public class Activities
{
   public Activities(string name, CheckBox chb, bool sel)
    {
        ActivityName = name;
        AcivityCheckBox = chb;
        Selected = sel;
    }

    private string _activityName;
    private CheckBox _activityCheckBox;
    private bool _selected;    

    public string ActivityName
    {
        get { return _activityName; }
        set { _activityName = value; }
    }
    public CheckBox AcivityCheckBox
    {
        get { return _activityCheckBox; }
        set { _activityCheckBox = value; }
    }
    public bool Selected
    {
        get { return _selected; }
        set { _selected = value; }
    }
}

Here is my DaoDailyActivities.cs

public class DaoDailyActivities
{
    string activityName = "";
    string userActivityName = "";

    bool IsSelected;
    int activityID;

    SqlConnection con = new SqlConnection("server=172.16.32.68;database=ParentalHealth;uid=sa;pwd=Emids123");

    public ObservableCollection<Activities> GetActivities()
    {

        SqlCommand cmd = new SqlCommand("SP_GetActivities", con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Connection = con;            
        con.Open();

        ObservableCollection<Activities> activitiesList = new ObservableCollection<Activities>();

        SqlDataReader readerActivities;
        readerActivities = cmd.ExecuteReader();

        while (readerActivities.Read())
        {
           activityID = Convert.ToInt32(readerActivities["ActivityID"]);
           activityName = readerActivities["ActivityName"].ToString();

           CheckBox chb = new CheckBox();

           activitiesList.Add(new Activities(activityName, chb, IsSelected = (activityID % 2 == 0)));

        }

        return activitiesList;

    }
    public List<UserActivity> GetUserActivity()
    {
        string sqlQuery = "SELECT ActivityName FROM Activities A ";
        sqlQuery+="INNER JOIN UserActivity UA ON A.ActivityID = UA.ActivityID WHERE UA.UserID = 1";
        SqlCommand cmd = new SqlCommand(sqlQuery, con);
        cmd.Connection = con;
        con.Open();

        List<UserActivity> userActivityList = new List<UserActivity>();

        SqlDataReader readerUserActivities;
        readerUserActivities = cmd.ExecuteReader();

        while (readerUserActivities.Read())
        {
            userActivityName = readerUserActivities["ActivityName"].ToString();
            userActivityList.Add(new UserActivity(userActivityName));
        }
        return userActivityList;
    }               
}

Here is my MainViewModel.cs

public class MainViewModel : ViewModelBase
{
    public string Welcome
    {
        get
        {
            return "Welcome to MVVM Light";
        }
    }

    public SeniorActivities.ViewModel.BusinessImplementation.SeniorActivities sn = new BusinessImplementation.SeniorActivities();


    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        string abc="hello";
        OpenPopUp = new RelayCommand(() =>
        {
            Activities pop = new Activities(ref abc);

            WindowInteropHelper helper = new WindowInteropHelper(pop);
            pop.ShowDialog();

        });

        ClosePopUp = new RelayCommand(() =>
        {
            Activities close = new Activities();
            WindowInteropHelper closePopUp = new WindowInteropHelper(close);
            close.Close();

        });

    }


    public RelayCommand OpenPopUp
    {
        get;
        private set;
    }
    public RelayCommand ClosePopUp
    {
        get;
        private set;
    }
}

Here is my Activities.xaml.cs

public partial class Activities : Window
{
    /// <summary>
    /// Initializes a new instance of the SeniorActivities class.
    /// </summary>
    public Activities()
    {
        DaoDailyActivities act = new DaoDailyActivities();
        InitializeComponent();
        listBox1.ItemsSource = act.GetActivities();

    }
    public Activities(ref string xyz)
    {
        DaoDailyActivities act = new DaoDailyActivities();
        InitializeComponent();
        listBox1.ItemsSource = act.GetActivities();
        MessageBox.Show(xyz);
    }
}

Im my DaoDailyActivities.cs

GetUserActivity() is returning the activities per specific user. I want this list with preselected checkboxes along with the list returning by GetActivities() in my second window so that user can check/uncheck the activities. All this I am storing/retrieving in sql server 2008

Kindly Suggest?

Thanks

Tarun
A: 

@Veer. Thanx Veer for the great help. It is working as per the requirement. Now am able to check/uncheck the checkboxes and values are going in the database. But the other thing is that I have the parent window with the Add New Button. By clicking on button, we can add more activities from the child window and the child window is getting closed. Now after making the selection in child window, parent window should reload or refresh itself or contents as per the selection made in child window so that we can see the updated records in the parent.

Kindly Help?

Thanks

Tarun
@Tarun: First of all that was more of a comment and has to be posted in the comment area. Second one is that you can make a new question out of this so that you would get many peers to help. Third is that you should illustrate your question better.
Veer