archimed01

Hello,

I have a problem with ItemsPanelTemplate.

I fill a ListView with for exemple, 100 textblock. The Scrollbar works correctly .

But i want to change by code the ItemsPanelTemplate.

If i use :

Code Snippet

Dim FE As FrameworkElementFactory = New FrameworkElementFactory(GetType(WrapPanel))

Dim paneltemplate As New ItemsPanelTemplate
paneltemplate = New ItemsPanelTemplate(FE)

ListBox1.ItemsPanel = paneltemplate



The panel is changed but the scrolling is not working


Same thing if i use resource :

Code Snippet

<ItemsPanelTemplate x:Key="WrapPanel">
<WrapPanel > </WrapPanel>
</ItemsPanelTemplate>





......

ListBox1.ItemsPanel = Me.Resources("WrapPanel")




Thanks for your help



Re: Windows Presentation Foundation (WPF) How to change ItemsPanelTemplate Correctly ?

Thomas C. Huber

Hi archimed,

do you need to change the template at runtime

I've the same effect. It's interesting. When you put your code into the Initialized-Event of the ListView, it will work.

I've created an example, instead of the FrameworkElementFactory class (which is deprecated) I use the recommended Load-Method of XamlReader to create an ItemsPanelTemplate in code.

Example with Initialized-Event. Scrollbar works correctly:

Code Snippet

<Window x:Class="WindowsApplication8.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WindowsApplication8" Height="300" Width="300" Loaded="WindowLoaded"

>

<ListView Initialized="ListViewInitialized" x:Name="lv"/>

</< FONT>Window>

Code Snippet

...

private void WindowLoaded(object sender, RoutedEventArgs e)

{

// Just fill the ListView with some TextBlocks. One for each Brush in the Brushes-Class

PropertyInfo[] pi = typeof(Brushes).GetProperties(BindingFlags.Static | BindingFlags.Public);

for (int i = 0; i < pi.Length; i++)

{

TextBlock tb = new TextBlock();

tb.Text = pi[i].GetValue(null, null).ToString();

tb.Background = (Brush)pi[i].GetValue(null, null);

lv.Items.Add(tb);

}

}

private void ListViewInitialized(object sender, EventArgs e)

{

StringReader sr = new StringReader(

"<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><WrapPanel/></ItemsPanelTemplate>"

);

XmlReader xml = XmlReader.Create(sr);

lv.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(xml);

lv.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);

}

...

Now I use exactly the same project. But instead of the Initialized-Event of the ListView, i install the Loaded-Event with the same code I had in the Initialized-Handler before.

And this doesn't show a scrollbar for the WrapPanel:

Code Snippet
<
ListView Loaded="ListViewLoaded" x:Name="lv"/>

Code Snippet

private void ListViewLoaded(object sender, EventArgs e)

{

StringReader sr = new StringReader(

"<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><WrapPanel/></ItemsPanelTemplate>"

);

XmlReader xml = XmlReader.Create(sr);

lv.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(xml);

lv.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);

}

Let's wait for the others. :-)






Re: Windows Presentation Foundation (WPF) How to change ItemsPanelTemplate Correctly ?

Yi-Lun Luo - MSFT

Hello, this simple sample works for me:

<ItemsPanelTemplate x:Key="ipt">

<WrapPanel/>

</ItemsPanelTemplate>

<ListBox ItemsPanel="{StaticResource ipt}"/>

I added 100 items, and they showed in a single horizontal line. The HorizontalScrollBar shows correctly. However, I think this is not the result you want. Since you’re using a WrapPanel, You’ll Probably want the items to show on a second line if they reach the edge of the screen. Then please use:

<ListBox ItemsPanel="{StaticResource ipt}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>






Re: Windows Presentation Foundation (WPF) How to change ItemsPanelTemplate Correctly ?

Thomas C. Huber

Hi Yi-Lun Luo,

yes, that's the way it works. But think of an application, where you want the user to change the itemspaneltemplate at runtime and you want to implement this in an EventHandler in Code. If you change the ItemsPanelTemplate in Code, the scrollbars doesn't work. How could we do that






Re: Windows Presentation Foundation (WPF) How to change ItemsPanelTemplate Correctly ?

Yi-Lun Luo - MSFT

In your case, you can just wrap your ListBox in a ScrollViewer. Please also note that generally it’s recommended to use resources defined in XAML rather than create XAML in code. You can refer to archimed01 to see how to access resources in code.






Re: Windows Presentation Foundation (WPF) How to change ItemsPanelTemplate Correctly ?

thomas gilbert

Hello,

I've got the same problem, I'd like to change the content of the ItemsPanelTemplate at runtime.


First of all, is it possible If i can, how could i do
Is there a solution

Feel free to help me Wink

Thx, Thomas