【问题标题】:How to add multiple items in same line to list box c#?如何将同一行中的多个项目添加到列表框c#?
【发布时间】:2014-02-07 12:10:17
【问题描述】:

我需要在列表框的一个冒号中设置两个元素,但我只能使用此代码设置一个:

<ListView> 
    <ListView.ItemTemplate>
        <DataTemplate>
            <Rectangle Width="13" Height="13" Name="Rectangles"  Margin="0,5,0,0" Fill="Red" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我也想在同一行的矩形旁边设置标签。

我该怎么做?谢谢

【问题讨论】:

    标签: c# listbox items


    【解决方案1】:

    DataTemplate 内创建Grid 并在ListView 内为您的控件创建ColumnDefinations

    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="13" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Rectange Grid.Column="0" Fill="Red"  Margin="0,5,0,0"/>
                <Label Grid.Column="1"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
    

    【讨论】:

      【解决方案2】:

      尝试将其放置在 Stackpanel 或 Grid 等容器项中

      <ListView>
        <ListView.ItemTemplate>
          <DataTemplate>
            <StackPanel>
              <Rectangle Width="13" Height="13" Name="Rectangles"  Margin="0,5,0,0" Fill="Red" />
              <TextBlock />
            </StackPanel>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
      

      【讨论】:

        【解决方案3】:

        你可以试试stackpanel

        例子:

        <ListView> 
            <ListView.ItemTemplate>
                <DataTemplate>
                 <StackPanel Orientation="Horizontal">
                         <Rectangle Width="13" Height="13" Name="Rectangles"  Margin="0,5,0,0" Fill="Red" />
                         <Label Content="MyContent" />
                 <StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        

        重要的部分是堆栈面板的方向,如果设置为水平,项目堆叠而不是向下。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-24
          • 2014-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多