C# 如何获取复选框“;勾选“;运行时在列表框内

C# 如何获取复选框“;勾选“;运行时在列表框内,c#,windows-phone-8,checkbox,C#,Windows Phone 8,Checkbox,我正在创建windows phone 8应用程序,我在列表框中有复选框,当我单击复选框时如何使复选框“选中” </StackPanel> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 我尽了最大努力但没有得到结果? 请参见下面的我的代码:

我正在创建windows phone 8应用程序,我在列表框中有复选框,当我单击复选框时如何使复选框“选中”

                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
我尽了最大努力但没有得到结果? 请参见下面的我的代码:

  <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>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我在我的列表框中的emulator上尝试了这个,效果很好。文本框被选中和取消选中,没有任何问题。

        <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>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

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

我得到了解决方案:

                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
 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();          
    }
答案如下:

                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
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;
    }

什么实际上不起作用?您的代码看起来很好(快速查看)。(好吧,除了你要设置的部分被选中,而不是得到它,但我想你知道你正在这么做)。@yasen问题是在designer视图中,在emulator中,我希望在我单击复选框时选中复选框。我的问题是,你能看到复选框选中,但什么都没有发生,或者你甚至看不到复选框有任何视觉变化吗?我的意思是,例如,当您选中空圆圈时,它会被填满?@Zgrkpnr是的,即使我选中它,复选框也会保持为空,不会有任何视觉变化!非常感谢。帮帮我。Apostolos,谢谢你的回复,我在列表框中有复选框,当点击它时仍然没有“勾选”。你能发布你的代码吗?非常感谢!!