在DataTemplate内部显示数据绑定StackPanel [英] Displaying a data-bound StackPanel inside a DataTemplate
问题描述
我有一些数据绑定到WPF中的ListBox的对象.最终结果应如下所示:
I have objects I'm databinding to a ListBox in WPF. Here's what the end result should look like:
-------------------------------
| Name | Opt1 | Value1 |
| | Opt2 | Value2 |
| | Opt3 | Value3 |
| | Opt4 | Value4 |
-------------------------------
基本上,我为整体变量准备了一个DataTemplate,然后Opt/Value组合具有它自己的DataTemplate.我正在尝试尽可能简单地显示值列表.
Essentially i've got a DataTemplate for the overall variable, and then the Opt/Value combo has it's own DataTemplate. I'm trying to display the list of values as simply as possible.
<Label Content="{Binding Path=Identifier.Name, Mode=OneWay}" />
<ListView Grid.Column="1" HorizontalAlignment="Stretch"
ItemsSource="{Binding Path=Values, Mode=OneWay}" />
Values的绑定当前仅是带有2个<Label>
的<Grid>
,并且ListView具有许多我不关注的功能,例如边框样式,选择等,而我真正想要的只是能够使用列表进行数据绑定.
The binding for Values is currently only a <Grid>
with 2 <Label>
's and ListView has a lot of features I dont watch, such as the border styling, selections, and such, when all I really want is to be able to databind using a List.
我尝试将项目数据绑定到堆栈面板,但是无法使其在XAML中工作.我想另一种解决方案是做我正在做的事情,并为ListView重写<Style>
.关于正确的方法有什么建议吗?
I've tried to databind the items to a stackpanel but couldn't get it to work in XAML. I suppose another solution is to do what I'm doing, and rewrite the <Style>
for ListView. Any suggestions on the correct way to do this?
推荐答案
听起来确实可以使用 IsSharedSizeScope 附加属性,以使我们的列保持格式均匀.另外,请查看ListBox链接底部的继承层次结构",它可以帮助您确定不同情况下所需的列表类型.
It certainly sounds like something you can do with a ListBox, or an ItemsControl if you do not want them to be selectable. We can also make use of the IsSharedSizeScope attached property to keep our columns formatted and even. Also, take a look at the Inheritance Higharchy at the bottom of the ListBox link, it should help you determine which type of list you need for different scenarios.
尝试这样的事情:
<DockPanel>
<Label Content="{Binding Path=Identifier.Name, Mode=OneWay}" />
<ListBox ItemsSource="{Binding Path=Values, Mode=OneWay}"
Grid.IsSharedSizeScope="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="OptionColumn" />
<ColumnDefinition SharedSizeGroup="ValueColumn" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Option}" />
<TextBlock Grid.Column="1" Text="{Binding Value}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
这篇关于在DataTemplate内部显示数据绑定StackPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!