Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击日历控件上的事件_C#_Wpf_Events - Fatal编程技术网

C# 单击日历控件上的事件

C# 单击日历控件上的事件,c#,wpf,events,C#,Wpf,Events,我在日历控件上添加了一个单击事件。但在我的实现中,这个事件不起作用 Cal.cs控件中的我的代码: #region click public static RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Cal)); public event RoutedEventHandler Click

我在日历控件上添加了一个单击事件。但在我的实现中,这个事件不起作用

Cal.cs控件中的我的代码:

#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代码:

<Calen:Cal x:Name="Calendar" Margin="0,50,0,0" Click="Calendar_Click"/>
我没有找到任何解决办法。我不知道为什么这个代码不能正常工作


您能帮我解决这个问题吗?

您需要将
DataContext
设置为指向包含隐藏代码的类

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

DataContext=“{Binding RelativeSource={RelativeSource Self}}}”>
这是必要的,因为在默认情况下,
DataContext
没有在WPF中正确设置

有关
DataContext
的更多信息,请参阅

请看,那里的共识是“不,他们不应该”!
<UserControl>
   DataContext="{Binding RelativeSource={RelativeSource Self}}">
</UserControl>