C# 将事件或命令分配给ResourceDictionary中的DataTemplate
我有以下课程:C# 将事件或命令分配给ResourceDictionary中的DataTemplate,c#,wpf,xaml,C#,Wpf,Xaml,我有以下课程: public class Day { public int Date { get; set; } public String DayName { get; set; } public Day() { } public Day(int date, string dayName) { Date = date; DayName = dayName; CommandManage
public class Day
{
public int Date { get; set; }
public String DayName { get; set; }
public Day()
{
}
public Day(int date, string dayName)
{
Date = date;
DayName = dayName;
CommandManager.RegisterClassCommandBinding(typeof(Day), new CommandBinding(DayClick, new ExecutedRoutedEventHandler(OnExecutedDayClick),
new CanExecuteRoutedEventHandler(OnCanExecuteDayClick)));
}
public static readonly RoutedCommand DayClick = new RoutedCommand("DayClick", typeof(Day));
private static void OnCanExecuteDayClick(object sender, CanExecuteRoutedEventArgs e)
{
((Day)sender).OnCanExecuteDayClick(e);
}
private static void OnExecutedDayClick(object sender, ExecutedRoutedEventArgs e)
{
((Day)sender).OnExecutedDayClick(e);
}
protected virtual void OnCanExecuteDayClick(CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = false;
}
protected virtual void OnExecutedDayClick(ExecutedRoutedEventArgs e)
{
string content = String.Format("Day {0}, which is {1}, was clicked.", Date.ToString(), DayName);
MessageBox.Show(content);
e.Handled = true;
}
}
我正在使用以下DataTemplate(位于ResourceDictionary中)来呈现它:
<DataTemplate DataType="{x:Type local:Day}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Rectangle Grid.ColumnSpan="2" x:Name="rectHasEntry" Fill="WhiteSmoke"/>
<TextBlock Grid.Column="0" x:Name="textBlockDayName" Text="{Binding DayName}"
FontFamily="Junction" FontSize="11" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,2,0,0"/>
<TextBlock Grid.Column="1" x:Name="textBlockDate" Text="{Binding Date}"
FontFamily="Junction" FontSize="11" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,2,0,0"/>
<Rectangle Grid.ColumnSpan="2" x:Name="rectMouseOver" Fill="#A2C0DA" Opacity="0"
Style="{StaticResource DayRectangleMouseOverStyle}">
</Rectangle>
</Grid>
</DataTemplate>
到目前为止没有问题,我可以在屏幕上看到它
我想能够做的是分配一个命令,或者使用一个事件,这样当用户在当天单击时,它将通知Day对象的父对象它已被单击
我尝试了以下方法:
<Rectangle.CommandBindings>
<CommandBinding Command="{x:Static local:Day.NextDay}"
Executed="{x:Static local:Day.OnExecutedDayClick}"
CanExecute="{x:Static local:Day.OnCanExecuteDayClick}"/>
</Rectangle.CommandBindings>
尝试并绑定Day类中的命令,但不起作用。我得到一个错误声明:
“ResourceDictionary”根元素需要一个x:Class属性来支持XAML文件中的事件处理程序。删除已执行事件的事件处理程序,或向根元素添加x:Class属性
我认为这意味着ResourceDictionary没有代码隐藏文件,或者类似的东西
无论如何,我不确定我是否应该在这里使用命令,或者以某种方式将事件绑定到所讨论的矩形,或者这是否可能。我看到过很多地方,看起来确实是有可能的,我只是无法将我所看到的转化为实际可行的东西,因此这篇文章
提前感谢。您不能在此处声明CommandBinding,在这种情况下,您可以在DataTemplate中在此处分配命令,并在主窗口或页面中声明CommandBinding 编辑: 通过这种方式,您可以在自定义控件中使用命令。 创建自定义控件,并在控件本身内部声明命令和命令绑定,如本示例所示 MyCustomControl.cs 在generic.xaml中编写此样式并分配如下命令: generic.xaml
点击
如果我想让它成为一个存在于CustomControls程序集中的控件,以便可以在其他应用程序中重复使用,那么我应该在哪里声明commandbindings?因为单击代码总是相同的,我希望它由控件处理,而不是由使用它的应用程序的主窗口或页面处理。
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
InitializeCommands();
}
private static RoutedCommand _myCommand;
public static RoutedCommand MyCommand
{
get
{
return _myCommand;
}
}
private static void InitializeCommands()
{
_myCommand = new RoutedCommand("MyCommand", typeof(MyCustomControl));
CommandManager.RegisterClassCommandBinding(typeof(MyCustomControl),
new CommandBinding(_myCommand , OnMyCommandExecute));
}
private static void OnMyCommandExecute(object sender, ExecutedRoutedEventArgs e)
{
MyCustomControl control = sender as MyCustomControl;
if (control != null)
{
//logic for this command
}
}
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<Grid>
<RepeatButton Command="{x:Static local:MyCustomControl.MyCommand}" >Click</RepeatButton>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>