c# - XAML 布局 ItemsControl

标签 c# wpf xaml

我在 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>

我现在想在每个复选框的右侧添加一个文本框,使其水平对齐,每行有 1 个复选框和 1 个文本框。我以为我必须使用 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>

我得到一个错误对象“DataTemplate”已经有一个 child ,不能添加“TextBox”

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

enter image description here

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

最佳答案

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

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

关于c# - XAML 布局 ItemsControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12807491/

相关文章:

c# - 为什么 WPF Presentation 库在 StringBuilder.ToString() 中包装字符串?

wpf - 为什么选项卡控件在更改选项卡时重用 View 实例

c# - 编译器找不到方法名

c# - 可拖动的 silverlight 用户控件

c# - 检查 XElement 是否全局为 null

wpf - MVVM View 优先方法的问题

c# - 如何从 Windows 应用商店应用程序中的 SecondaryTile 导航到特定页面?

wpf - Background "Red"中的字符串="Red"如何转换为SolidColorBrush?

c# - 导航属性的包含可以使用 EF6 嵌套在 Web API 中吗?

c# - 在 StreamWriting 到同一文件后立即 StreamReading 文件