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

  • SEARCH
  • Home
  • 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 116325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:09:37+00:00 2026-05-11T03:09:37+00:00

I have run into an issue with WPF and Commands that are bound to

  • 0

I have run into an issue with WPF and Commands that are bound to a Button inside the DataTemplate of an ItemsControl. The scenario is quite straight forward. The ItemsControl is bound to a list of objects, and I want to be able to remove each object in the list by clicking a Button. The Button executes a Command, and the Command takes care of the deletion. The CommandParameter is bound to the Object I want to delete. That way I know what the user clicked. A user should only be able to delete their ‘own’ objects – so I need to do some checks in the ‘CanExecute’ call of the Command to verify that the user has the right permissions.

The problem is that the parameter passed to CanExecute is NULL the first time it’s called – so I can’t run the logic to enable/disable the command. However, if I make it allways enabled, and then click the button to execute the command, the CommandParameter is passed in correctly. So that means that the binding against the CommandParameter is working.

The XAML for the ItemsControl and the DataTemplate looks like this:

<ItemsControl      x:Name='commentsList'     ItemsSource='{Binding Path=SharedDataItemPM.Comments}'     Width='Auto' Height='Auto'>     <ItemsControl.ItemTemplate>         <DataTemplate>             <StackPanel Orientation='Horizontal'>                 <Button                                                  Content='Delete'                     FontSize='10'                     Command='{Binding Path=DataContext.DeleteCommentCommand, ElementName=commentsList}'                      CommandParameter='{Binding}' />             </StackPanel>                                 </DataTemplate>     </ItemsControl.ItemTemplate> </ItemsControl> 

So as you can see I have a list of Comments objects. I want the CommandParameter of the DeleteCommentCommand to be bound to the Command object.

So I guess my question is: have anyone experienced this problem before? CanExecute gets called on my Command, but the parameter is always NULL the first time – why is that?

Update: I was able to narrow the problem down a little. I added an empty Debug ValueConverter so that I could output a message when the CommandParameter is data bound. Turns out the problem is that the CanExecute method is executed before the CommandParameter is bound to the button. I have tried to set the CommandParameter before the Command (like suggested) – but it still doesn’t work. Any tips on how to control it.

Update2: Is there any way to detect when the binding is ‘done’, so that I can force re-evaluation of the command? Also – is it a problem that I have multiple Buttons (one for each item in the ItemsControl) that bind to the same instance of a Command-object?

Update3: I have uploaded a reproduction of the bug to my SkyDrive: http://cid-1a08c11c407c0d8e.skydrive.live.com/self.aspx/Code%20samples/CommandParameterBinding.zip

  • 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. 2026-05-11T03:09:37+00:00Added an answer on May 11, 2026 at 3:09 am

    I stumbled upon a similar problem and solved it using my trusty TriggerConverter.

    public class TriggerConverter : IMultiValueConverter {     #region IMultiValueConverter Members      public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)     {         // First value is target value.         // All others are update triggers only.         if (values.Length < 1) return Binding.DoNothing;         return values[0];     }      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)     {         throw new NotImplementedException();     }      #endregion } 

    This value converter takes any number of parameters and passes the first of them back as the converted value. When used in a MultiBinding in your case it looks like the following.

    <ItemsControl      x:Name='commentsList'     ItemsSource='{Binding Path=SharedDataItemPM.Comments}'     Width='Auto' Height='Auto'>     <ItemsControl.ItemTemplate>         <DataTemplate>             <StackPanel Orientation='Horizontal'>                 <Button                                                  Content='Delete'                     FontSize='10'                     CommandParameter='{Binding}'>                     <Button.Command>                         <MultiBinding Converter='{StaticResource TriggerConverter}'>                             <Binding Path='DataContext.DeleteCommentCommand'                                      ElementName='commentsList' />                             <Binding />                         </MultiBinding>                      </Button.Command>                 </Button>             </StackPanel>                                                 </DataTemplate>     </ItemsControl.ItemTemplate> </ItemsControl> 

    You will have to add TriggerConverter as a resource somewhere for this to work. Now the Command property is set not before the value for the CommandParameter has become available. You could even bind to RelativeSource.Self and CommandParameter instead of . to achieve the same effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a day before end of project i run into a complicated issue: i have
I'm in the process of teaching myself WPF, and I have run into a
I have run into an issue with JQuery form submissions and have found no
I'm currently redesigning a website and have run into an issue with some elements
I seem to have run into a strange issue whereby a string reference is
I never actually thought I'd run into speed-issues with python, but I have. I'm
I have run into a common, yet difficult problem. I do not use Photoshop
I have run into a bit of a tricky problem in some C++ code,
I have run into a few cases where i have been asked to deploy
it seems I have run into a problem with Internet Explorer 7. I have

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.