Creating dynamic checkboxes against a list of items using mvvm light wpf?

I have the following script:

I have one window that says MainWindow, where I show a list of actions according to a specific user from the database. There is one button in the window. By clicking on this button, a new window opens with a complete list of actions from the main table. Now I want to add a chechbox in the second window for each element dynamically, so that the user can select / cancel the action. Those selected / canceled values ​​should be stored in the database, and Parent / MainWindow should be updated after clicking the "Finish" button, and the changes should be reflected in MianWindow. But I don’t understand how to dynamically create checkboxes for each list item and xaml binding and select / deselect the checkbox.

Please offer samples or examples.

thank

+3
source share
1 answer

You can customize your listviewitem using the ListView ItemTemplate . Add a check box and a text block to the panel that will make up your data table.

Update

Model:

public class Activity
{         
    public Activity(int id, string name) 
    { 
        ID = id;
        Name = name;        
    } 

    public int ID { get; set; }
    public string Name { get; set; }    

}

ViewModel for ListViewItem in the second window:

public class ActivityViewModel
{
    Activity _model;

    public ActivityViewModel(Activity model, bool isSelected) 
    { 
        _model = model;
        IsSelected = isSelected; 
    }        

    public string Name { get { return _model.Name; } }

    /* Since the view has a checkbox and it requires a bool value for binding
       we create this property */
    public Nullable<bool> IsSelected { get; set; }       
}

DataAccess

public class DaoDailyActivities 
{ 
    string activityName = "";  
    bool IsSelected; 

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

    public IEnumerable<Activity> GetActivities() 
    {  
        SqlCommand cmd = new SqlCommand("SP_GetActivities", con); 
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open(); /* It is safe to open connections in a try block */

        SqlDataReader readerActivities = cmd.ExecuteReader();
        while (readerActivities.Read()) 
        {           
            yield new Activity(readerActivities["ActivityID"].ToString(), readerActivities["ActivityName"].ToString());            
        } 
    }         
} 

ViewModel for SecondWindow:

public class SecondWindowViewModel : ViewModelBase 
{
    DaoDailyActivities _rep = new DaoDailyActivities();

    public ObservableCollection<ActivityViewModel> AllActivities { get; set; }

    public SecondWindowViewModel()
    {
        LoadAllActivities();
    } 

    LoadAllActivities()
    {
        foreach(Activity activity in _rep.GetActivities())
        {
            AllActivities.Add(new ActivityViewModel(activity, (activity.ID % 2 == 0)));
        }
    }
}

XAML:

<ListView ItemsSource="{Binding AllActivities}">
    <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=Name}" />
         <CheckBox IsChecked="{Binding Path=IsSelected}" />
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate> 
</ListView>
+3
source

Source: https://habr.com/ru/post/1752791/


All Articles