如何访问列表选择器的数据模板中的元素

时间:2014-04-15 06:00:02

标签: windows-phone-8

点击选择所有选项列表选择器必须选中复选框,无法访问堆栈面板中的复选框。我甚至尝试过可视化树,但没有获得

的Page1.xaml

         <StackPanel Orientation="Horizontal" Grid.Row="0">
        <CheckBox Width="75" BorderThickness="0" Checked="CheckBox_Checked_1"></CheckBox>
        <TextBlock Text="Select All" FontSize="38" Margin="0,13,0,0" Width="138" Foreground="Red"  />
    </StackPanel>
    <ListBox ItemsSource="{Binding}"  Name="lpk1" Grid.Row="1" Margin="0,-10,0,10">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox Width="75" BorderThickness="0" Name="chk1" ></CheckBox>
                        <TextBlock Text="{Binding name}" FontSize="34" Margin="0,13,0,0" Width="138" Foreground="Red"  />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>   

Page1.cs

              void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
       {
           try
           {
               allbrands = JsonConvert.DeserializeObject<AllBrand>(e.Result);
               lpk1.DataContext = allbrands.GetBrands;
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
       {


       }

1 个答案:

答案 0 :(得分:1)

在这里你拥有它:

ListBoxItem lboi = (ListBoxItem)lbo.ItemContainerGenerator.ContainerFromIndex(0);
StackPanel stack = lboi.Content as StackPanel;
CheckBox chk = stack.Children[0] as CheckBox;