Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Ensure Event Failed" error occur

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomCalc">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">

                    <TextBlock Text="welcome" Height="50" Width="150" MouseDown=""/>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

In the above program, (in the program i workout in custom control library) I am trying to change the control textblock => button.(textblock act as button functionality) so i try to add a event to the textblock it gives a error message "ensure event failed", And this file name is "Generic.xaml" so i add a class "Generic.xaml.cs" but same error is show on. Please explain why it should occur and how to resolve it, thanks in advance.

like image 789
MVK Avatar asked Oct 13 '15 05:10

MVK


Video Answer


1 Answers

You need to add a x:Class attribute to support event handlers in the XAML file. So your Generic.xaml should look like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomCalc"
    x:Class="CustomCalc.Generic">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <TextBlock Text="welcome" Height="50" Width="150" MouseDown="TextBlock_MouseDown"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

And as for the Generic.xaml.cs :

namespace CustomCalc
{
    public partial class Generic : ResourceDictionary
    {
        private void TextBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

        }
    }
}

Also don't forget to merge your ResourceDictionary in the App.Xaml file:

<Application.Resources>
    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/CustomCalc;component/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
like image 155
Bahman_Aries Avatar answered Oct 18 '22 15:10

Bahman_Aries