views:

716

answers:

5

Hello all,

Sorry if this is a basic question, but how can I take an ItemTemplate that I have for a ListBox, and put it in the resources for the window so that more than one ListBox can use it.

Here's some XAML:

<Window x:Class="Example">
    <Window.Resources>
        <DataTemplate x:Key="dtExample">
            <ListBox.ItemTemplate>
            // styles go here...
            </ListBox.ItemTemplate>
        </DataTemplate>
    </Window.Resources>
    <ListBox ItemTemplate="{StaticResource dtExample}">
    // items go here...
    </ListBox>
</Window>

This is throwing a "Attached property has no setter" design-time error. I've removed portions of code that I didn't think would matter, for sake of brevity.

Thanks

+3  A: 

just add your itemtemplate to your window's resource and add a key:

<Window.Resource>
 <DataTemplate x:Key="myTemplate">
  ....
 </DataTemplate>
</Window.Resources>

and then apply it with something like this:

<ListBox ItemTemplate="{StaticResource myTemplate}">
 ...
</ListBox>
Joachim Kerschbaumer
I tried putting my ListBox.ItemTemplate xaml inside of a DataTemplate, but get the red squiggly error, Attached Property has no setter. Ideas?
well, the ItemTemplate property of an ItemsControl IS a datatemplate. if you provide some code/markup it would be much easier to help you. just stating you get an error won't help ;)
Joachim Kerschbaumer
@Joachim Kerschbaurner: You're right!
A: 

I think the problem is that you should x:Key properties in your resources instead of the x:Name..

Change that, and it will work like a charm :)

Arcturus
I changed it to a key, and that stopped the ItemTemplate from being greyed out in the xaml, but I still get the error:The attachable property 'ItemTemplate' was not found in type 'ListBox'....and I have the ItemTemplate="{StaticResource myTemplate}" in there, as described above.
A: 

Do you have the following tags in your Window class?

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Arcturus
Yes indeedy. Thanks to all who read this! Yay community!
Hmm if it still does not work.. can you perhaps post your whole xaml file?
Arcturus
(Which is to say, yes, I do have those tags in my Window class). :)
Yikes, I'd like to omit the parts which aren't causing the problem, and that I shouldn't share...I'll comment some things out and and try to duplicate the problem again...and edit in some 'sanitized' xaml here in a bit. Clearly something funky is going on.
+2  A: 

you provided the following code:

 <DataTemplate x:Key="dtExample">
        <ListBox.ItemTemplate>
        // styles go here...
        </ListBox.ItemTemplate>
    </DataTemplate>

but this will not work. you cannot provide <ListBox.ItemTemplate> directly within your template. you don't need this here. just create a simple datatemplate and it should work.

Joachim Kerschbaumer
A: 

I know that the post is too old to be interesting for the author, yet i may be interesting for those who have the same problem and google it. As I may see the problem is you should use ListBox.ItemTemplate inside ListBox. For instance, ...

Kirill Lykov