Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

如何运用WP7中的可视化树查找控件中的某个特定元素

From Wiki
Jump to: navigation, search
WP Metro Icon UI.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

代码示例
兼容于
文章
翻译:
WS - OtomiiLu
最后由 hamishwillee 在 03 Jul 2013 编辑

跳转到: navigation, search 本文阐述了如何运用WP7中的可视化树查找控件中的某个特定元素。它用到了VisualTreeHelper,它是一个提供了遍历对象关系(沿子对象或父对象的轴)方法的实用类。

Contents

简介

VisualTreeHelper类在我们查找父控件内的一个特定的元素/子控件,做一些特定工作,如更改/删除子控件时,很用用。父控件可以是任何数据连接控件,如Grid、ListBox、StackPanel,子控件包括TextBox、Buttons、TextBlocks。 本代码示例中的简单应用显示:一个父控件ListBox 包含了几个 CheckBox。本例用VisualTreeHelper来导航ListBox,计数并列出被选中的条目。

	Screen1.png

示例应用程序页

	Screen3.png

底部的Textblock提供计数, 以及与每个选项关联的数据。

入门

首先创建一个WP应用。 打开Visual Studio并从已安装的模版中选择Windows Phone Application。 选择Windows Phone 7.1作为目标版本。

定义 ListBox

ListBox是在XAML中定义,并用C#代码数据绑定来填充的。XAML定义如下:

<ListBox x:Name="my_list" Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<CheckBox x:Name="cbx_state" Checked="cbx_state_Checked" Unchecked="cbx_state_Unchecked" Tag="{Binding}"/>
<TextBlock x:Name="txt_string" Text="{Binding}" VerticalAlignment="Center" FontSize="34" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

该ListBox 条目的内容/外观是依据Data Template定义的;鉴于此,每一个元素是一个CheckBox和一个StackPanel中水平对齐的 TextBlock。注意上面的XAML不显示用于显示结果的控件。 该ListBox是从下面的C#代码填充的,下面的代码设置了声明在XAML中StackPanel的内容:

my_list.ItemsSource = new List<string>() { "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6" };

计数已选择的 Checkboxs

下面我们定义一个查找特定元素/子控件的常用方法:

//Variables to store the count of checked Checkboxes and their data
string option_selected = "";
int check_count = 0;
 
//SearchElement populates above variables for checkboxes in specified "targeted_control"
private void SearchElement(DependencyObject targeted_control)
{
var count = VisualTreeHelper.GetChildrenCount(targeted_control); // targeted_control is the listbox
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targeted_control, i);
if (child is CheckBox) // specific/child control
{
CheckBox targeted_element = (CheckBox)child;
if (targeted_element.IsChecked == true)
{
if (option_selected != "")
{
option_selected = option_selected + " , " + targeted_element.Tag;
}
else
{
// get the value associated with the "checked" checkbox
option_selected = targeted_element.Tag.ToString();
}
// count the number of "Checked" checkboxes
check_count = check_count + 1;
return;
}
}
else
{
SearchElement(child);
}
}
}
else
{
return;
}
}

SearchElement()方法每当在CheckBox选中或 取消时被调用。如果复选框被选中:

private void cbx_state_Checked(object sender, RoutedEventArgs e)
{
check_count = 0;
option_selected = "";
SearchElement(my_list);
txt_count.Text = " Total Selected Checkboxes : " + check_count.ToString()
+Environment.NewLine + " Value Associated : " + option_selected;
}

如果复选框被取消选中:

private void cbx_state_Unchecked(object sender, RoutedEventArgs e)
{
check_count = 0;
option_selected = "";
SearchElement(my_list);
txt_count.Text = " Total Selected Checkboxes : " + check_count.ToString()
+Environment.NewLine + " Value Associated : " + option_selected;
}

资源下载

你可以从这里下载代码示例:

File:PhoneApp.zip

This page was last modified on 3 July 2013, at 03:58.
67 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×