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.
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 sureDepedentUpon
attribute is set on XAML file. It will look like this:On a side note, simple steps to make it work like that are:
UserControl
in your project. It will automatically do this work for you which I mentioned above.ResourceDictionary
. (Replace UserControl to ResourceDictionary).UserControl
toResourceDictionary
.