2012-03-15 49 views
1

我的列表框在WPF工作。我想在水平方向显示列表框。我的代码是我需要的列表框的水平视图

<Grid> 

    <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto"> 
     <ItemsControl x:Name="list" > 
      <ItemsControl.ItemTemplate> 
       <HierarchicalDataTemplate> 
        <Border Padding="5,0,0,2"> 
         <WrapPanel Orientation="Horizontal"> 
          <ListBox Name="mylistBox" Width="200" Height="200"> 
           <Label Content="{Binding name}"/> 
           <Label Content="{Binding phone}"/> 
           <Label Content="{Binding email}"/>          
           <TextBox Name="NameTxt" Width="20" Height="20" Text="{Binding Path=Contact1.name}"></TextBox> 
          </ListBox> 
         </WrapPanel> 
        </Border> 
       </HierarchicalDataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </ScrollViewer> 
</Grid> 

和我的程序看起来像图片(垂直) enter image description here 谁能告诉我怎样才能改变看法? 在此先感谢。

回答

5
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto"> 
    <ItemsControl x:Name="list" ItemsSource="{Binding Items}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel Orientation="Horizontal" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <HierarchicalDataTemplate> 
       <Border Padding="5,0,0,2"> 
        <ListBox Name="mylistBox" 
          Width="200" 
          Height="200"> 
         <Label Content="{Binding name}" /> 
         <Label Content="{Binding phone}" /> 
         <Label Content="{Binding email}" /> 
         <TextBox Name="NameTxt" 
           Width="20" 
           Height="20" 
           Text="{Binding Path=Contact1.name}" /> 
        </ListBox> 
       </Border> 
      </HierarchicalDataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</ScrollViewer> 
+0

完美......作品魅力 – 2012-03-15 13:19:47

+0

感谢Felix ..工作:) – Sampath 2013-10-29 13:19:08

1

尝试WrapPannel,这将出水平放置的物品,直到有没有更多的空间,然后移动到下一行。

你也可以使用一个UniformGrid,这将奠定项目出的行或列的定数。

我们得到一个ListView,ListBox中使用这些其他面板的项目,以人气指数的方式,或任何形式的ItemsControl的是通过改变ItemsPanel属性。通过设置ItemsPanel,你可以从ItemControls使用的默认StackPanel中改变它。使用WrapPanel我们也应该设置宽度,如下所示。

<ListView> 
    <ListView.ItemsPanel> 
     <ItemsPanelTemplate> 
       <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" 
            ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}" 
            MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}" 
            ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" /> 
     </ItemsPanelTemplate> 
    </ListView.ItemsPanel> 
... 
</ListView> 
+0

像你绑定的宽度和高度,真正使wrappanel包装的答案,但它可能会使其复杂的提问者,但+1包装渔获物。 – Silvermind 2012-03-15 13:01:59

4

您的itemscontrol没有提供自定义ItemsPanel,那么StackPanel被用作垂直方向的默认值。

试加:

<ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <StackPanel Orientation="Horizontal"/> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 
+0

+1最佳答案。 – Silvermind 2012-03-15 12:58:10

+0

@ kashmirilegion什么都没有奏效?请提供更多信息。它适用于我,所以你的问题没有提供足够的细节。你是如何试用我的解决方案的? – dowhilefor 2012-03-15 13:22:22

+0

@kashmirilegion你的意思确实能工作 – Silvermind 2012-03-15 13:26:16

2

您可以使用一个WrapPanel或根据您的要求一个StackPanel。

<ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel Orientation="Horizontal" /> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 

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

的文档IsItemsHost具有水平列表框的一个例子。

+0

您对IsItemsHost的使用是错误的。这不是伤害,而是错误的。当你像这样提供一个ItemsPanel时,它暗示它是ItemsHost,你不需要设置属性。但是,如果在ItemsControl的ControlTemplate中添加面板,则当然可以有多个面板。在那里,您可以使用该属性将您的ItemsHost标记为承载itemcontrols项目的面板。所以要么使用IsItemsHost要么使用ItemsPanel。 – dowhilefor 2012-03-15 13:20:44

+0

谢谢你指出。 – Phil 2012-03-15 13:31:17

1

我张贴,因为信息的目的是做事的另一种方式的这样的回答:

实体/职业:

public class Person 
    { 
    public string Name { get; set; } 

    public string Phone { get; set; } 

    public string Email { get; set; } 

    public Contact Contact1 { get; set; } 
    } 

    public class Contact 
    { 
    public string Name { get; set; } 
    } 

代码背后:

Persons = new List<Person>(); 
    for (int i = 0; i < 15; i++) 
    { 
    Persons.Add(new Person() 
       { 
        Name = String.Format("Name {0}" , i) , 
        Phone = String.Format("Phone 0000000-00{0}" , i) , 
        Email = String.Format("Emailaddress{0}@test.test" , i) , 
        Contact1 = new Contact { Name = String.Format("Contact name = {0}", i) } 
       }); 
    } 
    list.DataContext = Persons; 

的XAML建议1:

<ListBox x:Name="list" ItemsSource="{Binding}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
      <StackPanel Orientation="Vertical"> 
       <Label Content="{Binding Path=Name}"/> 
       <Label Content="{Binding Path=Phone}"/> 
       <Label Content="{Binding Path=Email}"/> 
       <TextBox Height="20" DataContext="{Binding Path=Contact1}" Text="{Binding Path=Name}" Width="110"/> 
      </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     </ListBox> 

XAML中建议2:

<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Visible"> 
     <ItemsControl x:Name="list" ItemsSource="{Binding}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
      <ListBox> 
       <Label Content="{Binding Path=Name}"/> 
       <Label Content="{Binding Path=Phone}"/> 
       <Label Content="{Binding Path=Email}"/> 
       <TextBox Height="20" DataContext="{Binding Path=Contact1}" Text="{Binding Path=Name}" Width="110"/> 
      </ListBox> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     </ItemsControl> 
    </ScrollViewer>