【问题标题】:Get checkbox value within a Listbox after submit button click单击提交按钮后获取列表框中的复选框值
【发布时间】:2014-09-29 09:25:04
【问题描述】:

我有一个带有以下列表框的数据透视页面。 仅供参考,列表框源是在调用并从 Web 服务获取后从后面的代码设置的。

<phone:PivotItem>
    <phone:PivotItem.Header>
        <TextBlock Text="1. Fault Reported" Style="{StaticResource pivotItemTitle}"/>
    </phone:PivotItem.Header>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>

        <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Visible" BorderBrush="#FFA68F8F">
            <ListBox x:Name="listBox_Fault" Width="455" Height="550">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="70">
                            <CheckBox VerticalAlignment="Center" Content="{Binding ItemName}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </ScrollViewer>

        <TextBlock Grid.Row="1" Style="{StaticResource pageNum}" Text="1/3"/>
    </Grid>
</phone:PivotItem>

在最后一个数据透视项目中,有一个带有点击事件的提交按钮。 单击按钮时,我想获取复选框的内容,并在它们被选中时将内容添加到列表中。

请问我如何访问这些复选框及其值?

【问题讨论】:

    标签: xaml windows-phone-8 mvvm silverlight-toolkit


    【解决方案1】:

    如果需要获取选中的复选框项,需要处理Checkbox中的“IsChecked”属性。首先在 xaml 中的复选框中添加以下行。

    IsChecked="{Binding Path=IsSelected,Mode=TwoWay}
    

    并在您的类中创建一个名为“IsSelected”的属性,如下所示,

    public bool IsSelected { get; set; }
    

    然后,在 submitbutton_click 事件中,使用以下代码。

    Items _items = new Items();
    Items.wList = listBox_Fault.Items.Cast<Items>().Where(li => (li.IsSelected)).ToList();
    

    其中,Items是你的类,wList是List属性如下,

    public static List<Items> wList;
    

    现在您将在名为 wList 的列表中获得选中的复选框值。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      相关资源
      最近更新 更多