未从链接到资源字典wpf的类触发控件事件

未从链接到资源字典wpf的类触发控件事件,wpf,resourcedictionary,uicontrolevents,Wpf,Resourcedictionary,Uicontrolevents,您好,我正在尝试将一个函数附加到文本框,用于输入从资源字典加载到接口的一些输入信息。这是XML <ContentControl> <Grid> <Image s:DesignerItem.Code ="1773" IsHitTestVisible="False" Stretch="Fill" Source="../Images/addition.png" ToolTip="Additio

您好,我正在尝试将一个函数附加到文本框,用于输入从资源字典加载到接口的一些输入信息。这是XML

          <ContentControl>
            <Grid>
                <Image s:DesignerItem.Code ="1773"  IsHitTestVisible="False" Stretch="Fill" Source="../Images/addition.png"  ToolTip="Addition" />
                <TextBox Height="57" Width="56" Margin="13,13,130,13" BorderThickness="0" FontSize="45" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" TextChanged="msg_TextChange" KeyUp="msg_MouseDown"/>
                <TextBox Height="57" Width="56" Margin="132,13,12,13" BorderThickness="0" FontSize="45" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" KeyDown="msg_MouseDown"/>
                <Button MouseDown="msg" Width="50" Height="20">ck</Button>
            </Grid>
           </ContentControl>
这是我正在使用的类的代码

 public partial class CodeBuilding : ResourceDictionary
{
   public CodeBuilding()
   {
      InitializeComponent();
   } 

   public void msg_TextChange(object sender,EventArgs e)
   {
       MessageBox.Show("oh no, you've got me ...");
   }

   public void msg(object sender, MouseEventArgs e)
   {
       MessageBox.Show("oh no, you've got me ...");
   }
}
如您所见,我只是使用一条简单的消息来指示事件是否已触发,项目成功构建并运行良好,但当我尝试触发任何 XML中使用的事件绑定到事件的函数根本不会触发


我不确定这是否是将函数链接到资源字典加载的事件的最佳方法,但有人能为我遇到的这个问题提供一些指导。

XAML文件应该依赖于它的部分代码隐藏文件,事件才能正常工作

确保将代码隐藏文件的生成操作设置为
Compile

还可以在记事本或任何文本编辑器中打开
.csproj
文件,并确保在XAML文件上设置了
dependent
属性。它将如下所示:

<Compile Include="CodeBuilding.xaml.cs">
  <DependentUpon>CodeBuilding.xaml</DependentUpon>
</Compile>

CodeBuilding.xaml

另一方面,让它像这样工作的简单步骤是:

  • 在项目中添加空白的
    UserControl
    。它将自动为您完成我上面提到的这项工作
  • 您只需将XAML文件更改为
    ResourceDictionary
    。(将UserControl替换为ResourceDictionary)
  • 在代码隐藏中,只需将基类从
    UserControl
    更改为
    ResourceDictionary

  • 嗨,Rohit,我尝试了您提供的这个解决方案,但是没有调用该函数。你还有其他建议吗?是的,我有。我将第二个解决方案添加到我正在处理的项目中,但它仍然没有触发任何消息窗口:(.
    ContentControl
    没有与之关联的
    x:Key
    。您如何在XAML文件中引用它?您可以发布复制问题的小样本吗?控件内容没有密钥,但它包装在另一个标记中,该标记附加了一个密钥,如下所示。
    ..
    我的意思是,您可以尝试在小样本中生成此场景,因为当我尝试使用simple button并钩住它的click事件时,它对我来说完全可以工作。。!!
    <Compile Include="CodeBuilding.xaml.cs">
      <DependentUpon>CodeBuilding.xaml</DependentUpon>
    </Compile>