【问题标题】:DataTemplate in ResourceDictionary and EventsResourceDictionary 和事件中的 DataTemplate
【发布时间】:2011-03-31 15:55:05
【问题描述】:

在我的 Silverlight 4 应用程序中,我有一个列表框,我为它创建了一个不错的 DataTemplate。此 DataTemplate 包含一些按钮,我想为其处理事件。所以我在模板上分配了事件:

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

在包含使用此模板的列表框的 UserControl 中,我有处理 myB_Click 的事件处理程序。

只要我在列表框中直接分配了模板,一切正常:

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

但是当我将 DataTemplate 外包给 ResourceDirectory 时,在将项目添加到列表框时出现运行时解析器错误

类别:ParserError
信息: 分配给属性时出错 'System.Windows.Controls.Button.Click'。

任何想法,这可能是什么原因造成的?

提前致谢,
弗兰克

【问题讨论】:

    标签: silverlight events data-binding


    【解决方案1】:

    如果你把它放在资源字典中,那么它就找不到事件处理程序,因为你的资源字典没有代码。

    要么您 (a) 不将该部分放入 ResourceDictionary 并将其保存在您的 xaml 中 或 (b) 在您的资源字典中添加代码

    【讨论】:

    • 嗯,我想我会选择一个。谢谢埃拉德!
    猜你喜欢
    • 2012-10-26
    • 2011-08-03
    • 2016-03-26
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多