Anton__

Hi

I'm looking for a way to lay out a set of custom UserControl objects using ItemsControl.
The trick is that I want to wrap each UserControl in some other controls defined in ItemTemplate. But for some reason it just ignores the ItemTemplate of the ItemsControl. Here is the XAML snippet:

Code Snippet

<ItemsControl ItemsSource="{Binding Source={StaticResource LtMenuCollection}}">
<ItemsControl.ItemTemplate>
<DataTemplate>

<Expander>

<Border BorderThickness="1" BorderBrush="Black"/>

<ContentPresenter ContentSource="Name" />

</Border>

<Expander>

</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>


LtMenuCollection is an ObservableCollection of UserControls defined as a resource.

So as Im saying I could never see the Expander and the Border.
I actually need that in order to create a common way to layout a set of different controls and to wrap them into an expander or whatever else. Is there some alternative way to do that

Thanks
Anton


Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

lee d

try taking the DataTemplate from within the Itemscontrol and move it to Resources section and reference it






Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

Anton__

Does not help =\

Here is what Im trying

Code Snippet

<ItemsControl ItemTemplate="{Binding Source={StaticResource MenuItemTemplate}}">
<menu:ManagerMenu />
<menu:EditMenu />
<menu:OtherMenu />
</ItemsControl>


I also just have tried
a) ItemTemplate="{StaticResource MenuItemTemplate}"
b) ItemTemplate="{DynamicResource MenuItemTemplate}"

Code Snippet

<Window.Resources>
<DataTemplate x:Key="MenuItemTemplate">
<Expander>
<Border BorderThickness="1" BorderBrush="Black">
<ContentPresenter />
</Border>
</Expander>
</DataTemplate>
</Window.Resources>


Im still getting plain controls without borders/expanders




Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

lee d

do you see anything in the output window, when you run the app. you will be better off setting the ItemsSource






Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

Anton__

Yes. I can see the my plain UserControls without expandes/borders arround them. I've tried creating a ObservableCollection and setting ItemsSource to it - does not help. But it does not really matter if the Items are set through ItemsSource or defined directly in the ItemsCounrol, does it




Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

lee d

yes, take a look at the sample

<Window x:Class="WindowsApplication1.Window3"

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

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

Title="WindowsApplication1" Height="300" Width="300"

>

<Window.Resources>

<DataTemplate x:Key="dt">

<Border Height="20" Width="20" Background="{Binding}"></Border>

</DataTemplate>

</Window.Resources>

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition/>

<ColumnDefinition/>

</Grid.ColumnDefinitions>

<ListBox Grid.Column="0" ItemTemplate="{StaticResource dt}">

<ListBoxItem>green</ListBoxItem>

<ListBoxItem>red</ListBoxItem>

<ListBoxItem>blue</ListBoxItem>

</ListBox>

<ListBox Grid.Column="2" ItemTemplate="{StaticResource dt}">

<ListBox.ItemsSource>

<x:Array Type="sysTongue Tiedtring"

xmlnsTongue Tiedys="clr-namespaceTongue Tiedystem;assembly=mscorlib">

<sysTongue Tiedtring>red</sysTongue Tiedtring>

<sysTongue Tiedtring>blue</sysTongue Tiedtring>

<sysTongue Tiedtring>green</sysTongue Tiedtring>

</x:Array>

</ListBox.ItemsSource>

</ListBox>

</Grid>

</Window>






Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

Anton__

It works for me to, when ItemsControl items area strings or some other simple objects. But when I try to layout other controls using ItemsControl - DataTemplate does not actually work.

Please Consider the following example. I'm actually not 100% sure what is the purpose of the ContentPresenter object though...

Code Snippet

<Window x:Class="WindowsApplication1.Window3"

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

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

Title="WindowsApplication1" Height="300" Width="300"

>

<Window.Resources>

<DataTemplate x:Key="dt">
<Expander>
<Border BorderThickness="1" BorderBrush="Black">
<ContentPresenter />
</Border>
</Expander>
</DataTemplate>

</Window.Resources>

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition/>

<ColumnDefinition/>

</Grid.ColumnDefinitions>

<ListBox Grid.Column="0" ItemTemplate="{StaticResource dt}">

<ListBoxItem><TextBlock Text="Red"/></ListBoxItem>

<ListBoxItem><TextBlock Text="Green"/></ListBoxItem>

<ListBoxItem><TextBlock Text="Blue"/></ListBoxItem>

</ListBox>

</Grid>

</Window>






Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

lee d

the datatemplate is not applied, you are just seeing the textblocks in the above example




Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

Anton__

Exactly. This is a problem actually. Do you have a clue why its not applied

Can anyone think of another way to achieve what I want - layout a set of controls according to the DataTemplate

Thanks
Anton




Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

lee d

As I said, create an object array and set Itemssource to that array or list. it will work






Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

Bragi

What might also work is: create a your own imlemation of ItemsControl. override:
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is XXX; //XXX = your custom wrapper object type
}

protected override DependencyObject GetContainerForItemOverride()
{
return new XXX(); //XXX = your custom wrapper object type
}




Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

Anton__

lee d, The result is really weird though...
Here is the code I tried:

Code Snippet

<ListBox ItemTemplate="{StaticResource dt}">
<ListBox.ItemsSource>
<x:Array Type="{x:Type Visual}">
<TextBlock Text="item1" Tag="heading1" />
<TextBlock Text="item2" Tag="heading2" />
<TextBlock Text="item3" Tag="heading3" />
</x:Array>
</ListBox.ItemsSource>
</ListBox>


Code Snippet

<Window.Resources>
<DataTemplate x:Key="dt">
<Expander Header="{Binding Path=Tag}">
<Border BorderThickness="1" BorderBrush="Black">
<ContentPresenter />
</Border>
</Expander>
</DataTemplate>
</Window.Resources>



I'm ending up with expanders with borders, but the content of each expander is another expander with another border, and recursively so on.
Here is the screenshot






Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

lee d

try this datatemplate

<DataTemplate x:Key="dt">

<Expander>

<Border BorderThickness="1" Height="100" Width="100" BorderBrush="Black">

<ContentControl Content="{Binding}"></ContentControl>

</Border>

</Expander>

</DataTemplate>






Re: Windows Presentation Foundation (WPF) Using ItemsControl to layout custom UserControls

Anton__

Hew....finally!!!
Thanks a lot, lee Smile