1

Hi I am attempting to attach a function to text box used for entering some input information which is loaded to an interface from resource dictionary. Here is the 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>

From the above code, I attempted to use a few different types of control events. I successfully linked the class my functions are going to be placed in using the following lines to link the class to the resource dictionary.

 x:Class="DiagramDesigner.CodeBuilding"
 x:ClassModifier="public"

Here is the code for the class I am using,

 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 ...");
   }
}

As you can see, I am just using a simple message to indicate if the event has been fired, the project successfully builds and runs fine, but when I attempt to trigger any of the events used in the XML the function tied to the event does not fire at all.

I am not certain if this is the best method of linking a function to an event loaded by a resource dictionary, but can anyone provide some guidance to this problem I am experiencing.

Daniel
  • 25
  • 7

1 Answers1

0

XAML file should be dependent on its partial code behind file for events to work.

Make sure Build Action for code behind file is set as Compile.

Also open your .csproj file in notepad or in any text editor and make sure DepedentUpon attribute is set on XAML file. It will look like this:

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

On a side note, simple steps to make it work like that are:

  1. Add a blank UserControl in your project. It will automatically do this work for you which I mentioned above.
  2. All you need to do is simply change the XAML file to ResourceDictionary. (Replace UserControl to ResourceDictionary).
  3. And in code behind just change the base class from UserControl to ResourceDictionary.
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Hi Rohit, I attempted this solution you presented, but the function was not called. Do you have any other suggestions? – Daniel Feb 09 '14 at 15:46
  • Yeah, I did. I added the second solution as an add on to the project I am working on and it still didn't fire any message window :(. – Daniel Feb 09 '14 at 16:22
  • `ContentControl` doesn't have an `x:Key` associated with it. How are you referring it in your XAML file? Can you post small sample replicating an issue? – Rohit Vats Feb 09 '14 at 16:24
  • The control content does not have a key, but it is wrapped in another tag that has a key attached to it, here is what it looks like. ` .... ` – Daniel Feb 09 '14 at 16:33
  • I meant can you try producing in small sample this scenario because when i tried with simple button and hook its click event, it works completely fine for me..!! – Rohit Vats Feb 09 '14 at 17:43
  • Rohit, thanks for all the time and assistance you have provided, but I cant seem to get the function to hook to control events :(. I would really liked to defer this to a chat, but that is presently beyond my capabilities. – Daniel Feb 12 '14 at 02:02
  • Try to replicate same issue in small sample because it works. Refer to my answer [here](http://stackoverflow.com/a/21709570/632337) which was working for the OP. – Rohit Vats Feb 12 '14 at 05:51