0

I have a ListView in which I need to add a fixed amount of items to in the XAML. (I don't need to dynamically add items to the list and I want to handle data binding from within the xaml as well.)

I understand how to add text as ListViewItems simply with <ListViewItem content="blah"/>

What I want to do is something like:

<ListViewItem><TextBox/><Label/></ListViewItem>

where the TextBox is appointed to the first column in my listview and the label to the second.

Is this at all possible to do within the XAML only? No code-behind. Would it be doable with a datagrid?

4

1 回答 1

0

如果您的 ListView 中只需要一行,多列,您可以将 ItemsPanel 更改为具有水平方向的 StackPanel

<ListView.ItemsPanel>
  <ItemsPanelTemplate>
    <StackPanel Orientation="Horizontal"></StackPanel>
  </ItemsPanelTemplate>
</ListView.ItemsPanel>

如果您的 ListView 中需要多行,您仍然可以通过使用堆栈面板作为 ListViewItem 并在其中放置子控件来将多个元素(水平)放置在一行中。

<ListView>
  <ListViewItem>
    <StackPanel Orientation="Horizontal">
      <TextBox></TextBox>
      <Label></Label>
    </StackPanel>
  </ListViewItem>
</ListView>

编辑:

要拥有具有某种类型控件的列,可以将 CellTemplate 分配给 GridView 的 Column。

<ListView>
<ListView.View>
    <GridView>
        <GridViewColumn Header="TextBoxColumn">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Anything}" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>
</ListView>

但是,您仍然需要从代码隐藏中填充网格视图。

于 2012-05-17T15:44:05.690 回答