从里面的DataTemplate Windows Phone 7的控制中获得价值(Getting v

2019-10-18 13:04发布

IAM工作的DataTemplate中的Windows Phone 7,我已经定制数据模板列表框。 现在我需要从列表框中定制获取值。 请检查代码如下。

     <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 correctly.the用户将检查提供的复选框所需要的物品。 现在我需要让我的代码的检查项目。 任何人可以帮助我这件事? 如何让检查的项目和项目值用户。

提前致谢。

Answer 1:

我已经解决了一整天后war.Here它goes.I得到了来自诺基亚开发者网站的解决方案。 你可以得到里面的DataTemplate复选框是否被选中,其相应的文字块的价值。

 //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;
        }
    }


文章来源: Getting value from a control inside DataTemplate windows phone 7