如何在运行时使ListBox内部的CheckBox“Ticked”

时间:2014-06-18 12:40:15

标签: c# windows-phone-8 checkbox

我正在创建windows phone 8应用程序,我在列表框中有复选框,当我点击复选框时如何获得复选框“CHECKED”?

我尽我所能但没有得到结果? 看下面的代码:

  <ListBox x:Name="listBox1" Width="429" Height="621" HorizontalAlignment="Left" Margin="21,43,0,59" VerticalAlignment="Top" ItemsSource="{Binding}" SelectedItem="{Binding}" SelectionChanged="listBox1_SelectionChanged" SelectionMode="Extended">
                <ListBox.ItemTemplate>
                    <DataTemplate>

            <Grid>
                <StackPanel Orientation="Vertical" Width="440">

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
                        <TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Margin="5" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
                    </StackPanel>

                    <StackPanel>
                        <TextBlock Text="Favorite" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="30" FontWeight="SemiBold" Margin="344,-35,9,0"/>
                    </StackPanel>

                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="Auto" Width="Auto" TextAlignment="Left" FontWeight="SemiBold"/>
                    </StackPanel>

                    <CheckBox x:Name="CheckBox1" Height="72" Foreground="Black" IsChecked="False" Margin="353,-40,28,0" BorderBrush="Black" Loaded="CheckBox1_Loaded" Checked="CheckBox1_Checked" Unchecked="CheckBox1_Unchecked"/>

                </StackPanel>
            </Grid>

            </DataTemplate>
                </ListBox.ItemTemplate>

            </ListBox>

这是我的代码隐藏文件:

 private void CheckBox1_Checked(object sender, RoutedEventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
        cb.IsChecked = true;
    }

4 个答案:

答案 0 :(得分:0)

我在列表框中的模拟器上尝试了这个,它运行正常。检查和取消选中文本框没有任何问题。

答案 1 :(得分:0)

        <ListBox.BorderBrush>
            <SolidColorBrush />
        </ListBox.BorderBrush>
        <ListBox.ItemTemplate>

            <DataTemplate>

               <Border BorderBrush="Black" BorderThickness="0,1,0,0" Width="600" Padding="0">
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,0">

                            <Image Stretch="UniformToFill" Source="{Binding Image}" Width="130" Height="130" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Image.Clip>
                                    <RectangleGeometry RadiusX="15" RadiusY="15" Rect="0,0,130,130" />
                                </Image.Clip>
                            </Image>

&lt; - CheckBox x:Name =&#34; CheckBox1&#34;高度=&#34; 72&#34;前景=&#34;黑色&#34;器isChecked =&#34;假&#34; BorderBrush =&#34;黑色&#34; / - &GT;                                                                                                                         

                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

答案 2 :(得分:0)

我得到了解决方案:

 private void CheckBox1_Checked(object sender, RoutedEventArgs e)
    {
        CONTACTS data = (sender as CheckBox).DataContext as CONTACTS;
        string contactId = data.contactId;
        string isFav = data.isFavourite.ToString();          
    }

答案 3 :(得分:0)

以下是答案:

private void CheckBox1_Checked(object sender, RoutedEventArgs e)
    {
        CLASS_NAME item = (sender as CheckBox).DataContext as CLASS_NAME;
        string id = item.Id;
        string name = item.Name;
    }