Click event for calendar management

I have added a click event to control the calendar. But with my implementation this event doesn'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 haven't found any solution. I don't know why this code is not working correctly.

Can you help me with this problem?

+3


source to share


1 answer


You need to set DataContext

to point to the class that contains the code behind.

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

      



This is necessary because, unfortunately, the default setting is DataContext

not configured correctly in WPF.

For more information on DataContext

see ReSharper WPF error: "Unable to resolve symbol 'MyVariable' due to unknown DataContext" .

0


source







All Articles