我有一个ListBox,可以拉入“程序”,可以多选一个复选框,指示它是否被选中。我想知道我的下一步是什么,我创建了IsChecked绑定但不知道如何将其与值相关联并将值保存到数据库。
xaml方:
<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方面:
private bool? programsIsSelected;
public bool? ProgramIsSelected
{
get { return programsIsSelected; }
set
{
programsIsSelected = value;
OnPropertyChanged("ProgramIsSelected");
}
}
我想要完成的事情(为了履行职责)
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);
}
}
上面的代码是我想要做的,但我遇到问题的部分是Populating
SelectedPrograms或Depopulating
选定的程序。
答案 0 :(得分:0)
你的最后一段很不清楚,你的问题没有提到Populating
或Depopulating
个对象/属性。
请澄清您的问题,以便我们更好地帮助您解决当前的问题。
与此同时,这里有一些代码:
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>
代码背后:
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>
{
}
}