Silverlight的列表框复选框所选项目 [英] Silverlight ListBox Checkbox Selected Item

查看:105
本文介绍了Silverlight的列表框复选框所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,有一个复选框作为项目模板的一部分。

I have a Listbox, that has a Checkbox as part of the item template.

在连的复选框的点击,我要确保相同的列表项被选中。

In the click even of the Checkbox, I want to make sure that same list item is selected.

ListBoxItem lbi = ((ListBoxItem)listLayers.
                       ContainerFromElement((CheckBox)sender));
lbi.IsSelected = true;

这是我遇到的主要问题是,ContainerFromElement是不是在Silverlight中使用。

The main issue that I'm having is that "ContainerFromElement" is not available in silverlight.

任何帮助是极大的AP preciated。

Any help is greatly appreciated.

修改

这是在code,我在里面我的列表中的复选框的Click事件中运行:

This is the code I'm running in the click event of a checkbox inside my list:

MyObject obj = listLayers.SelectedItem as MyObject;
obj.Visible = true;
obj.Value = "50";

使用的RelativeSource上的复选框与此code结合,我结束了OBJ等于空。

Using the RelativeSource binding on the checkboxes along with this code, I end up with obj equal to null.

我有,我想打开图层列表和关闭通过复选框,我愿意接受另一种方式......

I have a list of layers that I want to turn on and off via checkboxes, I'm open to another way...

推荐答案

在SelectionChanged事件,你必须走可视树找到的复选框。您可以通过使用做到这一点VisualTreeHelper

In the selectionChanged event you have to "walk" the visual tree to find the checkbox. You can do this by using the VisualTreeHelper

这个例子表明你需要做的就是该复选框。

This example show what you need to do to get to the checkbox.

下面是一些其他的办法解决这一问题

您应该使用ListBoxItem中的复选框之间的RelativeSource绑定。 该datatemple包含一个复选框。将它更改为这样的。

You should use a RelativeSource binding between the ListboxItem and the CheckBox. The datatemple contains a checkbox. Change it to look like this.

<CheckBox 
  IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, 
    Path=IsSelected, Mode=TwoWay}" />

这将创建列表框的 IsSelected 的属性和CheckBox的器isChecked 的属性之间的绑定。 <一href="http://blogs.veracitysolutions.com/blog/how-to-create-a-listbox-with-checkboxes-or-radiobuttons-in-silverlight-3/"相对=nofollow>这个教程介绍了如何用一个例子。

This creates a binding between the IsSelected property of the ListBox and the IsChecked property of the CheckBox. This tutorial explains how with an example.

如果您需要更多的控制,你应该看一看的行为,并触发。他们是一个有点复杂,但给你更多的控制。

If you need more control, you should have a look at behaviors and triggers. They're a bit more complex but give you you more control.

这篇关于Silverlight的列表框复选框所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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