绑定到复选框集合

时间:2013-03-31 14:50:36

标签: windows-phone-7 xaml

我正在尝试为Windows Phone做一些XAML绑定(针对WP7.1),我有一组我想要显示的复选框。我想将它们放在WrapPanel

我将使用哪些控件绑定到一组复选框?我没有看到WrapPanel的ItemSource。所以我不确定我会用什么。

   <ListBox Height="auto" Name="lbAssignments" BorderThickness="1" BorderBrush="Black" ItemsSource="{Binding DataList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock x:Name="TextBlock" Text="{Binding Title}"  HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,10" FontSize="26.667" TextWrapping="Wrap"/>
                    <TextBlock x:Name="TextBlock1" Text="{Binding Title}"  HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,10" FontSize="26.667" TextWrapping="Wrap"/>
                    <toolkit:WrapPanel Height="400" Width="400">
                        <!--collection of checkboxes-->
                    </toolkit:WrapPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

1 个答案:

答案 0 :(得分:3)

绑定到UI控件集合并不是您想要做的事情。相反,我建议绑定到一个集合。出于多种原因 - 性能,内存分配和一般维护/灵活性。

由于您提到自己有List<string>,因此您只需将其绑定到ListBox

<ListBox ItemsSource="{Binding YourList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding}"></CheckBox>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

但是,如果存在适当的支持,您仍然可以继续将元素绑定到其他元素的集合。您可以使用ItemsControl

<ItemsControl ItemsSource="{Binding ElementName=myPage, Path=SomeCollection}">

</ItemsControl>

此处,SomeCollection也可能是ObservableCollection<CheckBox>

相关问题