Hi,

I want to create a dependency property for both Command and CommandParameter so that I can use these in controls that don't support Command and CommandParameter like textblock etc.
I have written and taken help from someone to create DP(dependency property) for Command and its working fine but for CommandParameter, I am not sure how to proceed.
My Code is as follows :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Input;
using System.Reflection;
using System.Windows.Controls;

namespace InteractivityHelper
{
    public class InteractiveCommand : TriggerAction<DependencyObject>
    {
        protected override void Invoke(object parameter)
        {
            if (base.AssociatedObject != null)
            {
                ICommand command = this.ResolveCommand();
                if ((command != null) && command.CanExecute(parameter))
                {
                    command.Execute(parameter);
                }
            }
        }

        private ICommand ResolveCommand()
        {
            ICommand command = null;
            if (this.Command != null)
            {
                return this.Command;
            }
            if (base.AssociatedObject != null)
            {
                foreach (PropertyInfo info in base.AssociatedObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    if (typeof(ICommand).IsAssignableFrom(info.PropertyType) && string.Equals(info.Name, this.CommandName, StringComparison.Ordinal))
                    {
                        command = (ICommand)info.GetValue(base.AssociatedObject, null);
                    }
                }
            }
            return command;
        }

        private string _commandName;

        public string CommandName
        {
            get
            {
                base.ReadPreamble();
                return this._commandName;
            }
            set
            {
                if (this.CommandName != value)
                {
                    base.WritePreamble();
                    this._commandName = value;
                    base.WritePostscript();
                }
            }
        }

        #region Command

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public ICommand CommandParameter
        {
            get { return (ICommand)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(InteractiveCommand), new UIPropertyMetadata(null));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(ICommand), typeof(InteractiveCommand), new UIPropertyMetadata(null));

        #endregion
    }
}

Could someone please update my code and tell me how to proceed with the same ?

`

Recommended Answers

All 18 Replies

Any replies ???

Any replies ???

Clearly not. :rolleyes: Please don't bump your posts, it's rude.

Great, there are no replies still and I receive your comment not to bump my posts...So dear deceptikon, would you please tell me if there are no replies (like the previous 4 posts of WPF from me), what exactly I should be doing instead of asking help from someone ???
I joined this community 3 years ago and now you're telling me that clearly there are no replies....Well if there were no replies that's why I was waiting for the same...Being an Administrator does not mean that you critisize someone who asks for help (clearly that's why we're all here to help and ask for support).
Thanks again for no replies and commenting instead...!!!

You already asked for help. Either nobody knows the answer or nobody who knows the answer has bothered to reply. Nagging for replies because you're not getting them fast enough is more likely to be counterproductive.

Daniweb offers no guarantee of help, so if you're not getting the help you need, I'd suggest looking for alternatives. 6 days is a very long time on an active forum like this one, so I'd tend toward no longer expecting an answer.

Yeah well nobody has bothered to reply and I guess someone has send you to comment on this as no one has even bothered to reply...!!!
One side you're saying 6 days is a long time and on the other you said that I'm nagging for replies because I was not getting them fast enough (Are you even sure about what you are saying ???)
I never said that Daniweb offers any guarantee (3 years is a long time to understand that) and of course I'll search for alternatives as well after your kind reply...!!!
So, is this the way we all should go over this community meant for supporting people in the areas where they've got their expertise and you would tend towards no longer expecting an answer ???

Yeah well nobody has bothered to reply and I guess someone has send you to comment on this as no one has even bothered to reply...!!!

I merely noticed that you bumped all of your questions as I was perusing the forum.

One side you're saying 6 days is a long time and on the other you said that I'm nagging for replies because I was not getting them fast enough (Are you even sure about what you are saying ???)

Quite sure. 6 days is a long time on an active forum, which means if you haven't gotten a reply in that time, chances are good nobody knows the answer or wants to reply. I fail to see how this is difficult to understand.

I never said that Daniweb offers any guarantee (3 years is a long time to understand that)

Your behavior suggests that you feel entitled to answers. Is it unreasonable for me to mention that Daniweb makes no guarantee?

So, is this the way we all should go over this community meant for supporting people in the areas where they've got their expertise and you would tend towards no longer expecting an answer ???

It seems that your anger is getting in the way of rational thought. You posted a question to a group of volunteers. Nobody volunteered to answer. Now you're getting pissed off when I suggest that you shouldn't expect an answer and to consider alternatives.

So, you're saying I am getting pissed off when I am not getting answer/reply to my post and you expect me to appreciate your kind concern of suggesting alternatives, making comments and behaving like a frustrated Admin from my replies and you think its very nice of you to suggest alternatives to people asking for help and telling them that "you are getting pissed off Mr., why are you anyways here expecting for replies as there are still none".
I wonder who has made you the Admin of this site and of course I must commit that I am angry but its not getting in the way of my rational thoughts because still I am speaking logic and not getting pissed off with replies and telling people that "Mr. I am a professionally well mannered helpful admin but you...hey you are getting pissed off...!!!".
Thanks man.

Great, there are no replies still and I receive your comment not to bump my posts...So dear deceptikon, would you please tell me if there are no replies (like the previous 4 posts of WPF from me), what exactly I should be doing instead of asking help from someone

Instead of bumping the thread, you could have tried adding new information about what you have tried to get it working during those days you were waiting for an answer.

Additionally, the statement "its working fine but for CommandParameter" could be added to by you, explaining what you want to happen, and what is happening now. Clearly it doesn't work, but it is not clear what it is doing now, and what it should be doing.

Just my $0.02, so from now on, (everybody) please get back on topic.

Hi pritaeas,

The "Command" property is working fine but the "CommandParameter" is not working i.e., I can pass the events of the controls to the classes however I cannot pass the parameters that would be used in those events and thats where I am stuck and also, I have already mentioned what I need to do by creating these dependency properties, so please go through the first post and if possible please suggest any help for the same.

What exactly is not happening? I understand the part about not be able to pass events, but I'm not sure where from and where to.

I can't particularly see anything wrong with what you've done. The only thing that stands out is that you've not provided any callback information, essentially making them readonly properties of the control.

You should probably switch to using FrameworkPropertyMetadata or PropertyMetadata instead of UIPropertyMetadata (which is for Animation really).

Thanks a lot Ketsuekiame for your reply...Really appreciate this one.
Actually, I want to pass a UIElement Control (like x:Name/Name) in any of the events of a second control which does not support Command and CommandParameter as its properties (like TextBlock).
We have the following scenario :-

<Button Command="{Binding SomeCommand}" CommandParameter="{Binding ElementName=SomeControlName}" />

But we cannot have the same thing in a TextBlock and so to make it support I created this class with 2 dependency properties and used in my xaml like :-

<TextBlock>
 <i:Interaction.Triggers>
   <i:EventTrigger EventName="KeyDown">
      <interatComm:InteractiveCommand Command="{Binding DelegateCommandKey}" CommandParameter"{myDataGrid}" />
   </i:EventTrigger>
 </i:Interaction.Triggers>
</TextBlock>

But here at run time, I observed that the "Command" property is working perfectly fine like the default Command property present in the "Button" control whereas I caanot have the CommandParameter doing its job.

I may have written some incomplete code for the dependency property as I made such a property the first time due to which I am requesting that if anyone could possibly update that what exactly the code should be for the "CommandParameter" inside the dependency class or how do I need to proceed further.

This is also where I'm as lost as yourself. In your original code set I can't see any reason why it wouldn't work.

With respect to UIElement that doesn't mean you need to use a UIPropertyMetadata as you aren't dealing with Animation. Try switching them to FrameworkPropertyMetadata as you're actually dealing with WPF framework commands (binding).

Finally, just to eliminate a possible dual registration issues, did you try commenting out the CommandProperty declaration and registration to check if it will register the CommandParameterProperty one by itself.

Thanks for your quick response and following your suggestion I replaced the UIPropertyMetadata with FrameworkPropertyMetadata however how can it register if I comment out the following code ?

public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(ICommand), typeof(InteractiveCommand), new FrameworkPropertyMetadata(null));

Also, correct me if I am wrong, to use a dependency property we must register it so if I don't register how will I be able to access the CommandParameter property ?
Could you please provide some code snippet as an example?

I meant, comment out the CommandProperty not the CommandParameterProperty, essentially to check that it's not seeing these as the same property. It shouldn't, but you know, WPF... :/

We need to check if there is something wrong with the way you're registering the property (ie. It still doesn't work after you comment out the other one) or if the framework is getting confused by a double registration of the same type (even though it has a different name).

I don't think that the framework will be confused by double registration because we already have Command and CommandParameter defined for buttons and other controls.
How about viewing the existing Command and CommandParameter properties. Can we see them how they are defined as I am not sure about this one ?

My point still stands; we know that the way you've declared CommandProperty works, so we don't need it complicating matters.
There is a need to go over a process of elimination, no matter how we think it ought to work, we should perform the steps anyway.

Whilst it might not be that the framework is 'confused', if it suddenly starts working afterwards, then we have a more defined route to trouble shoot, rather than just guessing.

Hello, have you come up with any solution to this as I am also new to WPF and want to learn this one.

No, unfortunately this thread was sort of dead since many days but I would appreciate if someone could possibly help me around with this as I still haven't found any solution for the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.