我认为您没有按照目的正确使用Checkbox。
Checkbox 应该代表关于某个主题的状态(例如是/否)。不过,您只需要在选中复选框时使用Checked 事件,否则使用Unchecked。
所以在Checked事件中,获取你想要的内容。
编辑
您必须以某种方式使用 MVVM 模式来维护这一点。为此,互联网上有很多例子,我相信你可以处理。
不要使用Click="CheckBox_Click",而是使用Check 事件:
private void CheckBox_Checked (Object sender, EventArgs e)
{
var currentCheckBoxItem = sender as CheckBox;
if (currentCheckBoxItem.IsChecked == true)
{
//you manipulation here...
}
}
还是。这可能不起作用,因为您没有提供足够的细节。
编辑 2 一点点 MVVM...
首先,创建一个具有单个字符串属性的 Hobby 模型类(稍后您可能会改变主意以添加更多属性,Idk):
public class Hobby : INotifyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
OnPropertyChanged();
}
}
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
OnPropertyChanged();
}
}
//You can add some multiple properties here (***)
public Hobby (string hobbyName, bool isSelected)
{
Name = hobbyName;
IsSelected = isSelected;
}
//INotifiyPropertyChanged interface member implementation ...
}
(* ) 比如简短的描述然后绑定到View上。这种 MVVM 模式的主要优点是逻辑分离,因此如果必须更改某些内容,每个组件的分离会使其更容易。
其次,创建一个 ViewModel 类(你应该实现 INotifyPropertyChanged 接口):
public class HobbiesViewModel : INotifyPropertyChanged
{
private ObservableCollection<Hobby> _hobbies;
public ObservableCollection<Hobby> HobbiesCollection
{
get
{
return _hobbies;
}
set
{
_hobbies = value;
OnPropertyChanged();
}
}
//Constructor
public HobbiesViewModel
{
HobbiesCollection = new ObservableCollection<Hobby>();
}
//INotifyPropertyChanged interface member implementation ...
}
第三,创建 ViewModel 的实例(ObservableCollection)。使用此快速帮助:在App.xaml.cs 中,创建一个静态对象并根据需要通过应用程序使用它:
public partial class App
{
//This already exists in your app's code, but I've written it to
//make an idea where to write the Hobbies object
public static PhoneApplicationFrame RootFrame { get; private set; }
public static HobbiesViewModel Hobbies;
//Again, the already existing constructor
public App()
{
...
Hobbies = new HobbiesViewModel();
}
现在,您几乎已准备就绪;你有模型,你有视图模型,剩下的就是创建与视图的连接。这可以通过绑定轻松完成。 ViewModel 代表您的控件的DataContext(在您的情况下为LongListSelector,因此在该视图的(页面)构造函数中,编写以下语句:
yourListControlName.DataContext = App.Hobbies;
现在绑定是唯一剩下的东西了。这是在 XAML 代码中完成的。我不会在这里放一大堆 XAML 代码,因为您最清楚您的控件的外观。不过,从您提供的简短示例来看,只有一些调整:
列表 XAML 控件的项目源将绑定到代表控件的 DataContext 的 ViewModel 的 ObservableCollection 对象名称。有点糊是吧?为了更清楚,在这种情况下,您需要写ItemsSource="{Binding HobbiesCollection}",ObservableCollection。此外,在模板中,您应该拥有绑定在您的 Model 属性上的 CheckBox:
<DataTemplate>
<StackPanel Orientation="Horizontal"> //StackPanel is kinda useless if you have
//only one child control in it. But I wrote
//according to your code.
<Checkbox Content="{Binding Name}" IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
现在,我有点不清楚。为什么要使用复选框?我想到了下一个可能的场景:你通过 Json 数据的反序列化来获得一些你的爱好。要将它们添加到ViewModel,您只需:
App.Hobbies.HobbiesCollection.Add(new Hobby("firstHobbyFromJson", true));
App.Hobbies.HobbiesCollection.Add(new Hobby("secondHobbyFromJson", true));
这将使View 中已选择所有爱好。我想,你会添加一些其他的爱好,用户没有没有选择,现在可以添加它们:
App.Hobbies.HobbiesCollection.Add(new Hobby("aNewHobby", false));
App.Hobbies.HobbiesCollection.Add(new Hobby("anotherNewHobby", false));
此时,列表中包含用户以前的所有爱好以及您提供给他的一些新爱好。在他的选择完成后,如果你需要序列化只包含选择的爱好的Json,你可以这样:
var userHobbies = App.Hobbies.HobbiesCollection.Where(h => h.IsSelected);
或使用 foreach 并仅获取具有 IsSelected 属性为 true 的爱好对象。
祝你好运!