I have a ListBox that pulls in "Programs" and can be multi selected with a checkbox indicating if it's selected or not. I'm wondering what's my next step, I have create the IsChecked binding but don't know how to associate it with the value and save the values to a database.
xaml side:
<ComboBox Grid.Row="1" ItemsSource="{Binding VisPrograms}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Value}"
IsChecked="{Binding Path=ProgramsIsSelected, Mode=TwoWay}" Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}">
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
MVVM side:
private bool? programsIsSelected;
public bool? ProgramIsSelected
{
get { return programsIsSelected; }
set
{
programsIsSelected = value;
OnPropertyChanged("ProgramIsSelected");
}
}
What I want to accomplish (to clerify)
BindableCollection<Program> SelectedPrograms = new BindableCollection<Programs>
using (var ctx = DB.GET())
{
foreach (Program _program in SelectedPrograms)
{
Program NewProgram = new Program();
NewProgram.Person_Id = PersonId;
NewProgram.Value = _program.value;
ctx.Program.Add(NewProgram);
}
}
The code just above is what I want to do, But the part I have issues with is Populating
SelectedPrograms or Depopulating
selected programs.
Your last paragraph is quite unclear, nowhere on your question there is a Populating
or Depopulating
objects/properties.
Please clarify your question so we can better help you in addressing your current problem.
In the mean time here is some code that will:
XAML:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication7="clr-namespace:WpfApplication7"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<ComboBox x:Name="CheckBox1"
Width="100"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="wpfApplication7:MyItem">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
Code behind:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication7
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var collection = new MyItemCollection();
collection.Add(new MyItem {Name = "item1", IsChecked = true});
collection.Add(new MyItem {Name = "item2", IsChecked = false});
CheckBox1.DataContext = collection;
}
}
internal class MyItem : INotifyPropertyChanged
{
private bool _isChecked;
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
internal class MyItemCollection : ObservableCollection<MyItem>
{
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.