开发者

DataTemplate in ResourceDictionary and Events

开发者 https://www.devze.com 2023-02-21 18:54 出处:网络
in my Silverlight 4 application, I have a listbox for which I created a nice DataTemplate. This DataTemplate contains some b开发者_开发技巧uttons, for which I want to handle events. So I assigned the

in my Silverlight 4 application, I have a listbox for which I created a nice DataTemplate. This DataTemplate contains some b开发者_开发技巧uttons, for which I want to handle events. So I assigned the event on the template:

<DataTemplate>
  <Grid>
    <Button x:Name="myB" Click="myB_Click" />
  </Grid>
</DataTemplate>

In the UserControl that contains the listbox that uses this template I have the eventhandler that handles the myB_Click.

As long I have the template assigned directly within the listbox, everything works fine:

<ListBox ...>
  <ListBox.ItemTemplate>
    <DataTemplate>
      ...
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

But when I outsource the DataTemplate to a ResourceDirectory, I get a runtime parser-error when adding an item to the listbox

Kategorie: ParserError

Message: Error assigning to Property 'System.Windows.Controls.Button.Click'.

Any idea, what might cause this?

Thanks in advance,

Frank


if you put it in a resource dictionary then it can't find the event handler since your resource dictionary doesn't have code behind.

either you (a) don't put that part in the ResourceDictionary and keep it in your xaml or (b) add code behind to your resource dictionary

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号