XAML布局ItemsControl

use*_*963 2 c# wpf xaml

我在Google上进行了广泛搜索,并且很难找到一个简单的示例来跟踪ItemsControl,我认为我需要能够执行以下操作.

我目前有一个Grid,其中包含一个包含Checkboxes的可滚动列表框,它使用以下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>
Run Code Online (Sandbox Code Playgroud)

我现在想在每个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>
Run Code Online (Sandbox Code Playgroud)

此外,如果我尝试添加类似的文本框

    <DataTemplate>
        <CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
        <TextBox />
    </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

任何人都可以给我一些关于如何在XAML中构建控件以获得我想要的布局的指针吗?

Jos*_* C. 10

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

 <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
    <TextBox />
    </StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)