[英]XAML layout ItemsControl
我在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>
我现在想在每个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中构建控件以获得我想要的布局的指针吗?
只需在DataTemplate中使用StackPanel并将方向设置为水平。
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
<TextBox />
</StackPanel>
</DataTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.