Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Organising resources
Message
De
04/03/2009 05:49:41
 
 
À
03/03/2009 14:32:33
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01384902
Message ID:
01385413
Vues:
28
>>Hi,
>>
>>I'm looking at general design for a WPF application. There seems to be plenty of reading on property and style inheritance, user controls etc but very little on strategies for pulling the whole thing together as a type of framework. To get a feel for what can be done I've created a UserControl which wraps a couple of elements plus a button. The buttons click is used to raise a 'Clicked' event at the control level. Now I might need to use this as, say, a DataTemplate in a ListBox. So in Window.Resources I wrap it in a Style so:
>><Style x:Key="RectTemplate" TargetType="ItemsControl">
>>  <Setter Property="ItemsControl.ItemTemplate">
>>    <Setter.Value>
>>      <DataTemplate>
>>        <loc:TestListItem x:Name="TLI" Clicked="TestListItem_Clicked" />
>>      </DataTemplate>
>>    </Setter.Value>
>>  </Setter>
>> </Style>
and then use it for a Listbox:
<ListBox Grid.Row="1" Name="listBox1"  Style="{StaticResource RectTemplate}" SelectionMode="Extended"
>>      HorizontalContentAlignment="Stretch">
>></ListBox>
So far so good - everything works fine. But say that I now want to move the Style definition to an external or application level resource dictionary. The Clicked event handler can't now be hooked up because it is not in scope. So how can I hook it up in the Listbox on the form (even in code behind)?
>>Or is there a better way of architecting this sort of thing?
>>Regards,
>>Viv
>
>Few possible answers...
>
>1. Don't put DataTemplates in Styles.
>2. Create a Static Event Handler.
>3. Capture the Routed Event at the parent instead of capturing the event on the child.
>4. Use ItemContainerGenerator.ContainerFromItem to walk the list and setup the event handlers.
>
>In most cases, I use the third option. The 4th works, but if your data changes you will need to rewalk the list.
>
>This looks like a similar problem to my TreeView code.
>
>I have a custom TreeViewItem and I use the GetContainerForItemOverride so that each leaf uses my custom class.

Hi,
I really want to be able to encapsulate DataTemplates in Styles but I also don't see how not doing so would help in this case.

Doh. I'm not sure what you were suggesting in the 3rd option (changes in the UserControl or in the consuming form?) but just the mention of routed events made me realise that my UserControl Clicked event was not routed! Once that was changed so that it bubbled I could handle it as an attached event anywhere up the tree....

I've not familiar with the ItemContainerGenerator class. AFAIK I've never had occasion to use it (but of course with WPF until you know about it you don't know whether you've needed it or not :-})

I now currently dithering over whether to go for specifying a Key or a DataType in the DataTemplate.......

Thx,
Viv
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform