简体   繁体   English

在DataTemplate内部显示数据绑定StackPanel

[英]Displaying a data-bound StackPanel inside a DataTemplate

I have objects I'm databinding to a ListBox in WPF. 我有一些数据绑定到WPF中的ListBox的对象。 Here's what the end result should look like: 最终结果应如下所示:

-------------------------------
| Name    | Opt1    |  Value1 |
|         | Opt2    |  Value2 |
|         | Opt3    |  Value3 |
|         | Opt4    |  Value4 |
-------------------------------

Essentially i've got a DataTemplate for the overall variable, and then the Opt/Value combo has it's own DataTemplate. 本质上,我为整体变量提供了一个DataTemplate,然后Opt / Value组合具有它自己的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}" />

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. 目前,Values的绑定仅是带有2个<Label><Grid> ,并且ListView具有许多我不关注的功能,例如边框样式,选择等,而我真正想要的只是能够使用列表进行数据绑定。

I've tried to databind the items to a stackpanel but couldn't get it to work in XAML. 我尝试将项目数据绑定到堆栈面板,但无法使其在XAML中工作。 I suppose another solution is to do what I'm doing, and rewrite the <Style> for ListView. 我想另一种解决方案是做我正在做的事情,并为ListView重写<Style> Any suggestions on the correct way to do this? 关于这样做的正确方法有什么建议吗?

It certainly sounds like something you can do with a ListBox , or an ItemsControl if you do not want them to be selectable. 当然,这听起来像您可以使用ListBox或ItemsControl进行的操作(如果您不希望它们是可选的)。 We can also make use of the IsSharedSizeScope attached property to keep our columns formatted and even. 我们还可以利用IsSharedSizeScope附加属性来保持列的格式均匀。 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. 另外,请查看ListBox链接底部的“继承层次”,它可以帮助您确定不同情况下所需的列表类型。

Try something like this: 尝试这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM