从DataTemplate Windows Phone 7中的控件获取价值 [英] Getting value from a control inside DataTemplate windows phone 7

查看:68
本文介绍了从DataTemplate Windows Phone 7中的控件获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows Phone 7中处理dataTemplate。我已经使用数据模板自定义了Listbox。现在我需要从自定义列表框中获取值。请检查下面的代码。

Iam working on dataTemplate in windows phone 7. I had customised Listbox with data template. now i need to get the values from the customised listbox. Please check the code is below.

     <phone:PhoneApplicationPage.Resources>
    <Style x:Key="Image_List" TargetType="ListBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <Grid>
                        <Rectangle Fill="#FF030403" RadiusY="10" RadiusX="10"    Stroke="#1BA1E2" StrokeThickness="2"/>
                        <ScrollViewer x:Name="ScrollViewer">
                            <ItemsPresenter Height="Auto"/>
                        </ScrollViewer>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
 </phone:PhoneApplicationPage.Resources>
    <ListBox BorderBrush="#1BA1E2" Name="Image_Listbox" Margin="5,53,6,6" Style="{StaticResource Image_List}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border CornerRadius="5" BorderBrush="#1BA1E2" BorderThickness="1" Margin="3,1,0,0" Height="69" Width="445">
                        <StackPanel Margin="0,0,0,0" Background="Transparent"  Orientation="Horizontal" Height="69" Width="400">
                            <CheckBox Name="Images_Check" Margin="0,0,0,0" Content="" Height="67"  HorizontalAlignment="Left"/>
                            <TextBlock TextAlignment="Left" FlowDirection="LeftToRight" Width="Auto"  FontSize="22" Text="{Binding UBindData}" Height="40" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我正确获取了UI_Template。用户将在提供的复选框中选中所需的项目。现在我需要在代码中获取选中的项目。有人可以帮助我吗?用户如何获取选中的项目和项目值。

Iam getting the UI_Template correctly.the user will check the required Items in the checkbox provided. now i need to get the checked items in my code. can anybody help me out of this? How to get the checked Items and Itemvalue by the user.

预先感谢。

推荐答案

日战争。在这里,我从诺基亚开发人员站点获得了解决方案。您可以获取是否已选中datatemplate内部的复选框及其对应的Textblock值。

I had got solution after a day war.Here it goes.I got solution from nokia developer site. you can get whether checkbox inside datatemplate is checked and its corresponding Textblock value.

 //Variables to store the count of checked Checkboxes and their data
    public string option_selected = "";
    public int check_count = 0;

    //SearchElement populates above variables for checkboxes in specified "targeted_control"
    public 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 (targeted_element.Tag!= null)
                        {
                            // 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;
        }
    }

这篇关于从DataTemplate Windows Phone 7中的控件获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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