How do I change the layout of an ItemsControl  
Author Message
stswordman





PostPosted: Windows Presentation Foundation (WPF), How do I change the layout of an ItemsControl Top

I know a way to change the layout of the ItemsControl using ControlTemplate from the post
"How do I change the layout of an ItemsControl"

The post introduce tow way to achieve it:

1
<ListBox ItemsSource="{Binding Source={StaticResource xmlData}}" (...) >
<ListBox.ItemsPanel>
<StackPanel Orientation="Horizontal" />
</ListBox.ItemsPanel>
</ListBox>

2

<ControlTemplate x:Key="listBoxTemplate">
<Border BorderBrush="Orange" BorderThickness="2" Margin="10,0,10,10">
<StackPanel Orientation="Horizontal" IsItemsHost="True" />
</Border>
</ControlTemplate>

But first sample only run on CTP version.

And i want to know how to do it in version 3.0

could anyone help me
thanks



Visual Studio 200837  
 
 
Douglas Stockwell





PostPosted: Windows Presentation Foundation (WPF), How do I change the layout of an ItemsControl Top

You need to wrap the template in an ItemsPanelTemplate.

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


 
 
stswordman





PostPosted: Windows Presentation Foundation (WPF), How do I change the layout of an ItemsControl Top

Stockwell,thanks for your response?
I try your code,it works.
But when i add a border to the StackPanel,the applicaiton crashed...Why

<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Border>
<StackPanel Orientation="Horizontal" />
</Border>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>


 
 
lee d





PostPosted: Windows Presentation Foundation (WPF), How do I change the layout of an ItemsControl Top

I think as the error says it is looking for a panel. the error is 'VisualTree of ItemsPanelTemplate must contain a Panel. 'System.Windows.Controls.Border' is not a Panel.'

I think you can get the same effects by putting the listbox in a border



 
 
Bryan Wheeler -- MSFT





PostPosted: Windows Presentation Foundation (WPF), How do I change the layout of an ItemsControl Top

Ran into the same problem this morning. I discovered the other alternative is to specify both the ItemsPanelTemplate and the ControlTemplate. Everything but the panel goes in the ControlTemplate. Don't forget the <ItemsPresenter> in your ControlTemplate.

<ItemsControl ItemsSource="blah">

<ItemsControl.Template>

<ControlTemplate TargetType="ItemsControl">

<Border>

<ItemsPresenter/>

</Border>

</ControlTemplate>

</ItemsControl.Template>

<ItemsControl.ItemsPanel>

<ItemsPanelTemplate>

<StackPanel Orientation="Horizontal"/>

</ItemsPanelTemplate>

</ItemsControl.ItemsPanel>

</ItemsControl>

--Bryan

This posting is provided "AS IS" with no warranties, and confers no rights.