如何在 Windows Phone 应用程序中获取选中的复选框内容? [英] How to get checked checkbox content in windows phone app?

查看:20
本文介绍了如何在 Windows Phone 应用程序中获取选中的复选框内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Windows 手机应用程序.在应用程序中,我想放置多个复选框.我可以放置多个复选框.但是当我选中复选框时,我想要获取其内容(复选框内容).为此,我我使用检查事件和点击事件,但我不能得到我想要的结果.我的 xaml 代码如下:

I am developing windows phone app .In app,I want to put multiple check box.I able to put multiple check box.But when i checked on check box i want getting its content(check box content).For that i am use checked event and also click event but i cant get result as i want.My xaml code is as below:

        <ListBox Name="hobbylist" ItemsSource="{Binding}" Margin="0,0,10,10" >

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <StackPanel Orientation="Horizontal">

                        <CheckBox Name="hobbycheck" Content="{Binding Path=Title}"

                                  IsChecked="{Binding IsSelected}" ClickMode="Release" 

                Click="CheckBox_Click" ></CheckBox>
                    </StackPanel>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

    </Grid>

请帮帮我...

推荐答案

我认为您没有正确使用 Checkbox 根据其目的.

I think you are using the Checkbox not correctly according to its purpose.

Checkbox 应该代表关于主题的状态(例如是/否).尽管如此,您只需要在复选框被选中时使用 Checked 事件,否则使用 Unchecked 事件.

Checkbox should represent a state (e.g. yes/no) regarding a subject. Still, you just have to use the Checked event when the checkbox gets checked and Unchecked otherwise.

因此在 Checked 事件中,获取您想要的内容.

So in the Checked event, get the content you wish.

编辑

您必须以某种方式使用 MVVM 模式来维护它.为此,互联网上有很多示例,我相信您可以处理.

You have to maintain this with the MVVM pattern somehow. For that, there are plenty of examples in the internet, I am sure you can handle that.

不要使用 Click="CheckBox_Click",而是使用 Check 事件:

Instead of having Click="CheckBox_Click", use the Check event :

private void CheckBox_Checked (Object sender, EventArgs e)
{
    var currentCheckBoxItem = sender as CheckBox;
    if (currentCheckBoxItem.IsChecked == true)
        {
             //you manipulation here...
        }
}

还是.这可能只是行不通,因为您没有提供足够的问题细节.

Still. this might just not work, because you haven't provided enough details of your matter.

编辑 2 一点点 MVVM...

Edit 2 A little of MVVM...

首先,创建一个 Hobby 模型类,具有单个字符串属性(您可能稍后会改变主意添加更多属性,Idk):

First, make a Hobby model class, with a single string property (you might change your mind later to add more properties, 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 模式的主要优点是逻辑分离,因此如果必须更改某些内容,每个组件的分离会更容易.

(* ) For example, a short description and then bind it on the View. The major advantage of this MVVM pattern is logic separation, so if something has to change, the separation of each component makes it easier.

其次,创建一个ViewModel类(你应该实现INotifyPropertyChanged接口):

Second, create a ViewModel class (you should implement INotifyPropertyChanged interface) :

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 中,创建一个静态对象并根据需要通过应用程序使用它:

Third, create an instance of the ViewModel (the ObservableCollection). Use this quick help out : In the App.xaml.cs, create a static object and use it through the app as you need it :

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,因此在该视图的(页面的)构造函数中,编写以下语句:

Now, you almost have it all set; You have the Model, you have the ViewModel, all that's left is to create the connection with the View. This can be easily done through binding. The ViewModel represents the DataContext of your control (in your case the LongListSelector, so in that View's (Page's) constructor, write the following statement :

yourListControlName.DataContext = App.Hobbies;

现在只剩下绑定了.这是在 XAML 代码中完成的.我不会在这里放一大块 XAML 代码,因为您最了解控件的外观.尽管如此,根据您提供的简短样本来看,只有一些调整:

Now the binding is the only thing left. This is done in XAML code. I won't put a whole chunk of XAML code here, cause you know best how your control looks like. Still, judging by the short sample you provided, there a few adjustments only :

列表 XAML 控件的项目源将绑定到表示控件的 DataContextViewModelObservableCollection 对象名称.有点糊涂吧?为了更清楚,在这种情况下,您需要编写 ItemsSource="{Binding HobbiesCollection}",即 ObservableCollection.此外,在模板中,您应该有 CheckBox 绑定在您的 Model 的属性上:

The items source of the list XAML control will be bound to the ObservableCollection object name of the ViewModel representing the control's DataContext. A bit fuzzy, huh? To be clearer, in this case, you need to write ItemsSource="{Binding HobbiesCollection}", the ObservableCollection. Also, in the template, you should have that CheckBox which is bound on your Model's properties :

<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,您只需要:

Now, here things are a bit unclear to me. Why would you use the Checkbox? I've thought of the next possible scenario : You come with some string of your hobbies through deserialization of the Json Data. To add them to the ViewModel, you need only :

App.Hobbies.HobbiesCollection.Add(new Hobby("firstHobbyFromJson", true));
App.Hobbies.HobbiesCollection.Add(new Hobby("secondHobbyFromJson", true));

这将使 View 中已经选择的所有爱好.我想,你会添加一些其他的爱好,用户没有哪些未被选中,现在可以添加它们:

This would make all hobbies already selected in the View. I guess, you would add some other hobbies, the user doesn't have which are not selected and could add them now :

App.Hobbies.HobbiesCollection.Add(new Hobby("aNewHobby", false));
App.Hobbies.HobbiesCollection.Add(new Hobby("anotherNewHobby", false));

此时,用户在列表中拥有其以前的所有爱好,以及您提供给他的一些新爱好.他的选择完成后,如果你只需要把选择的爱好序列化Json,你可以这样:

At this point, the user has all its previous hobbies in the list and as well some new hobbies you provided him. After his selection is done, if you need to serialize the Json with only the selected hobbies, you could get like this :

var userHobbies = App.Hobbies.HobbiesCollection.Where(h => h.IsSelected);

或使用 foreach 并仅获取那些具有 IsSelected 属性为 true 的爱好对象.

or with a foreach and get only those hobby objects which have the IsSelected property as true.

祝你好运!

这篇关于如何在 Windows Phone 应用程序中获取选中的复选框内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆