Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 690731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:28:00+00:00 2026-05-14T02:28:00+00:00

I have the following class: public class Day { public int Date { get;

  • 0

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 am using the following DataTemplate (that is in a ResourceDictionary) to render 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>

No problems so far, I can get it on screen.

What I want to be able to do is assign a Command, or use an event, so that when the user clicks on the Day it will notify the parent of the Day object that it has been clicked.

I’ve 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 and bind the commands that are in the Day class but it didn’t work. I got an error stating:

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

Which I think means that there is no code-behind file for a ResourceDictionary, or something to that effect.

In any event, I’m not sure if I should be using Commands here, or somehow tying events to the Rectangle in question, or if this is even possible. I’ve seen various places where it sure looks like it’s possible, I’m just unable to translate what I’m seeing into something that actually works, hence this post.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:28:00+00:00Added an answer on May 14, 2026 at 2:28 am

    You cann’t declare CommandBinding here, in this case you can assign the command here in DataTemplate and declare CommandBinding in your main Window or Page.

    Edit:

    In this way you can use Commands with your custom control.
    Create a custom control and Declare Commands and Command Bindings also inside the control itself as in this Sample.

    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 commands like this:

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class public class UIControl { public string FName{ get; set;
I have the following class: public class Movie { string Name get; set; string
I have the following class public class Car { public Name {get; set;} }
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class: public class Account { public int AccountID { get;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following class: public abstract class AbstractParent { static String method() {
I have the following class: public class LooklessControl : Control { public List<int> IntList
I have the following class public class MyClass { private int myAttr; public void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.