2

我在 Google 上进行了广泛搜索,并且正在努力寻找一个简单的示例来遵循 ItemsControl,我认为我需要能够执行以下操作。

我目前有一个带有可滚动列表框的网格,其中包含复选框,使用以下 XAML 布局按预期工作

<Grid>
    <ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" 
ItemsSource="{Binding Selections}" Margin="12,22,12,94">
    <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked}" 
        Content="{Binding Path=Item.SelectionName}">
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

我现在想在每个 Checkbox 的右侧添加一个 TextBox,使其与每行 1 个 Checkbox 和 1 个 Textbox 水平对齐。我以为我必须使用 ItemsControl 来允许两个控件但使用 ItemsControl 如下我失去了滚动的能力

<Grid>
    <ItemsControl ScrollViewer.VerticalScrollBarVisibility="Auto" 
    ItemsSource="{Binding Selections}" Margin="12,22,12,94">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked}" 
        Content="{Binding Path=Item.SelectionName}">
                </CheckBox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

另外,如果我尝试添加一个类似于

    <DataTemplate>
        <CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
        <TextBox />
    </DataTemplate>

我收到一个错误The object 'DataTemplate' already has a child and cannot add 'TextBox'

本质上,我希望它看起来像这样

在此处输入图像描述

谁能给我一些关于如何在 XAML 中构造控件以获得我想要的布局的指示?

4

1 回答 1

10

只需在 DataTemplate 中使用 StackPanel 并将方向设置为水平。

 <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
    <TextBox />
    </StackPanel>
</DataTemplate>
于 2012-10-09T19:59:08.230 回答