xaml wpf(MVVM)中的两列自由流动布局

时间:2015-05-06 06:08:29

标签: c# wpf xaml

我有一个集合,我想在我的WPF appliaction中呈现为xaml中的两个或更多列布局。我不想使用网格列,因为我不想修复每列中的项目数。有什么方法可以使用包装面板,堆叠面板......等等。

我想要这样的东西

  Mango    Orange       Basil
  Banana   Pomegranate  Tulsi
  Apple    Papaya       ....

提前致谢。

2 个答案:

答案 0 :(得分:1)

您最好的选择是WrapPanel,您可以设置面板的宽度和项目的宽度。这样,您可以限制列数,而不会限制每列中的项目数量。

您的代码应如下所示:

    <WrapPanel Width="600"
           ItemWidth="200">
    <WrapPanel.Resources>
        <Style TargetType="TextBox">
            <Setter Property="BorderThickness" Value="0"/>
        </Style>
    </WrapPanel.Resources>
    <TextBox Text="Mango"/>
    <TextBox Text="Orange"/>
    <TextBox Text="Basil"/>
    <TextBox Text="Banana"/>
    <TextBox Text="Pomgranate"/>
    <TextBox Text="Tulsi"/>
    <TextBox Text="Apple"/>
    <TextBox Text="Papaya"/>
    <TextBox Text="..."/>
</WrapPanel>

哪个应该是这样的:

WrapPanel example

答案 1 :(得分:0)

您可以像这样使用ItemTemplate:

        <Stack Panel Width = "400">
<ItemTemplate ItemSource ="{Binding someModel}"
   <ItemsControl.ItemTemplate>
      <DataTemplate>
        <TextBlock>
         <Ran Text = "{Binding First}">
         <Ran Text = " " />
         <Ran Text = "{Binding Second}">
         <Ran Text = " " />
         <Ran Text = "{Binding Third}">
         <Ran Text = " " />
         </TextBlock>
      </DataTemplate>
 </ItemsControl.ItemTemplate>
</StackPanel>

在视图模型中,您可以这样写:

ObservableCollection<YourModel> someModel ...

,模型将是:

class YourModel
{
public string first  {get; set; }
public string second {get; set; }
public string third  {get; set; }
}
祝你好运:)