【发布时间】: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 中构造控件以获得我想要的布局的建议?
【问题讨论】: