Assigning an Event or DataTemplate to a ResourceDictionary

I have the following class:

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; } } 

I use the following DataTemplate (i.e. in a ResourceDictionary) to display it:

 <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> 

While there is no problem, I can get it on the screen.

What I want to do is assign a command or use an event so that when a user clicks on a Day, he notifies the parent of the Day object that he was clicked.

I tried the following:

 <Rectangle.CommandBindings> <CommandBinding Command="{x:Static local:Day.NextDay}" Executed="{x:Static local:Day.OnExecutedDayClick}" CanExecute="{x:Static local:Day.OnCanExecuteDayClick}"/> </Rectangle.CommandBindings> 

to try to link the teams that are in the Day class, but that didn't work. I got an error message:

The 'ResourceDictionary root element' requires an x: Class attribute to support event handlers in the XAML file. Either remove the event handler for the Executed event, or add the x: Class attribute to the root element.

What I think means there is no code file for code for ResourceDictionary. Something like that.

In any case, I'm not sure if I should use the commands here or somehow connect the events with the rectangle in question, or if it is possible. I saw different places where this seems possible, I just can’t translate what I see, something that really works, this post is from here.

Thanks in advance.

+6
c # wpf xaml
source share
1 answer

You cannot declare a CommandBinding here, in which case you can assign a command here in the DataTemplate and declare a CommandBinding in your main window or page.

Edit:

This way you can use Commands with your custom control. Create your own control and declare commands and command bindings also inside the control itself, as in this example.

MyCustomControl.cs

  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 } } 

and in your generic.xaml write this style and assign the commands as follows:

Generic.xaml

 <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> 
+4
source share

All Articles