3

I have added a click event on the calendar control. But with my implementation, this event don't work.

My code in Cal.cs control:

#region click
public static RoutedEvent ClickEvent =
EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Cal));

public event RoutedEventHandler Click
{
    add { AddHandler(ClickEvent, value); }
    remove { RemoveHandler(ClickEvent, value); }
}

protected virtual void OnClick()
{
    RoutedEventArgs args = new RoutedEventArgs(ClickEvent, this);
    RaiseEvent(args);
}

protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
    base.OnMouseLeftButtonUp(e);
    OnClick();
}
#endregion

XAML code :

<Calen:Cal x:Name="Calendar" Margin="0,50,0,0" Click="Calendar_Click"/>

C# code :

private void Calendar_Click(object sender, RoutedEventArgs e)
{
    string t = "";
}

I don't found any solution. I don't know why this code don't work correctly.

Can you help me with this problem please ?

Konamiman
  • 49,681
  • 17
  • 108
  • 138
Moussawi
  • 393
  • 1
  • 6
  • 22
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Jun 09 '15 at 06:55

1 Answers1

0

You need to set the DataContext to point to the class that contains the code behind.

<UserControl>
   DataContext="{Binding RelativeSource={RelativeSource Self}}">
</UserControl>

This is necessary because unfortunately, by default, the DataContext is not set up correctly in WPF.

For more info on DataContext, see ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext".

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305