获取复选框值mvvm light wpf

获取复选框值mvvm light wpf,wpf,mvvm,mvvm-light,Wpf,Mvvm,Mvvm Light,我有一个usercontrol,其中以按钮的形式显示数据库中的主要类别。通过单击主类别按钮,将打开一个新的用户控件,并单击相应类别中的项目列表。我使用TexbBlock在xaml中绑定该类别的项目名称,并显示每个要选择的项目的复选框。我想在选中复选框并单击“完成”按钮后获取它们的值。复选框值应为数据库中的项目ID。如何获取选中的复选框值 <!--<Button Content="{Binding CategoryName}" Marg

我有一个usercontrol,其中以按钮的形式显示数据库中的主要类别。通过单击主类别按钮,将打开一个新的用户控件,并单击相应类别中的项目列表。我使用TexbBlock在xaml中绑定该类别的项目名称,并显示每个要选择的项目的复选框。我想在选中复选框并单击“完成”按钮后获取它们的值。复选框值应为数据库中的项目ID。如何获取选中的复选框值

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
以下是用于相同目的的xaml:

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>

好心的建议

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
谢谢

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>

Tarun

我认为您需要做的是,在ViewModel上有一个名为“Checked”(或类似的东西)的布尔属性,然后将其绑定到您的复选框

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
IsChecked="{Binding Checked, Mode=TwoWay}"
“Mode=TwoWay”意味着当您选中复选框时,绑定将自动更新ViewModel上的属性

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>

希望这对Ben有所帮助。如何在单击按钮后将选中的复选框值保留在集合中。请建议?@Tarun,如果您将复选框绑定到视图模型,您可以简单地遍历集合并说“if MyViewModel.Checked Then…”。“有道理吗?”本。对我会试着让你知道。Thanks@Ben. 我试过那个,但没有成功。请详细说明。谢谢。你说不成功是什么意思?能否在选中的属性集上设置断点,并确保选中该复选框时正确设置属性值?您是否确实有此ViewModel的集合要迭代?
                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>