【问题标题】:XAML layout ItemsControlXAML 布局 ItemsControl
【发布时间】:2012-10-09 19:56:13
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢你,这种工作但不完全是。它将 TextBox 放在 Checkbox 上,但 Checkbox 超出了 TextBox。有没有办法将 Stackpanel 拆分为列 - 我将编辑我的 Q 以显示此图像。
    • 我有点昏暗,这实际上工作得很好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多