Question

CheckableCommandParameter Tag Binding

Posted 5 years ago by Avatar Owen Christensen - Software Engineer, Minitab Inc.
<ribbon:RibbonGallery Label="Gallery">
  <ribbon:RibbonGallery.ItemsSource>
    <x:Array Type="{x:Type mediaImaging:BitmapImage}">
      <mediaImaging:BitmapImage UriSource="Start.png" ribbon:RibbonControlService.CommandParameter="Start"/>
    </x:Array>
  </ribbon:RibbonGallery.ItemsSource>
  <ribbon:RibbonGallery.ItemTemplate>
    <DataTemplate>
      <Image Source="{Binding BindsDirectlyToSource=True}" Margin="2" Stretch="None" ribbon:RibbonControlService.CommandParameter="{Binding Path=(ribbon:RibbonControlService.CommandParameter), Mode=OneTime}"/>
    </DataTemplate>
  </ribbon:RibbonGallery.ItemTemplate>
  <ribbon:RibbonGallery.ItemContainerStyle>
    <Style TargetType="{x:Type ribbon:GalleryItem}">
      <Setter Property="Command" Value="{StaticResource MyCommand}"/>
      <Setter Property="CommandParameter">
        <Setter.Value>
          <ribbon:CheckableCommandParameter Tag="{Binding Path=(ribbon:RibbonControlService.CommandParameter), Mode=OneTime}"/>
        </Setter.Value>
      </Setter>
    </Style>
  </ribbon:RibbonGallery.ItemContainerStyle>
</ribbon:RibbonGallery>
What I'm trying to do is provide a string in addition to the checkableparameter which I thought could be achieved through styling my GalleryItem to bind the Tag to the attached CommandParameter. I get the following in my output window:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=(0); DataItem=null; target element is 'CheckableCommandParameter' (HashCode=26426675); target property is 'Tag' (type 'Object')

Am I doing something dumb here?

Comments (6)

Posted 5 years ago by Owen Christensen - Software Engineer, Minitab Inc.
I did a little looking up on this, and it looks like this already got hit elsewhere in these forums and on the internet in general. Apparently the CheckableCommandParameter, not being a FrameworkElement or Freezable, will not have an inheritance context set up for it, meaning my binding is useless. This may merit looking at. Making CheckableCommandParameter inherit from Freezable instead of DependencyObject might not be an unreasonable solution.

[Modified at 02/09/2009 10:05 PM]
Posted 5 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Owen,

I believe I've seen the post where they said an inheritance context is created for Freezables within a FrameworkElement. Is that what you were referring to?

I had tried a quick test to make a Freezable-based ICheckableCommandParameter but still got the same warning. Perhaps you could give it a try and see if you have any success.

Remember that you just need to implement ICheckableCommandParameter. If you do find a way around it, it let us know. Thanks!

Actipro Software Support
Posted 4 years ago by Markus Springweiler
Just make CheckableCommandParameter derive from Freezable and the "bindable" Tag-property will no more be entirely useless.

Why do such simple things take years?
Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Markus,

As we last posted, when we tried inheriting Freezable we still received the same binding error so it didn't seem to help and thus the change was not made. If you see differently then please e-mail us with a sample showing it working and we'll be happy to update things.

Actipro Software Support
Posted 4 years ago by Markus Springweiler
Deriving from Freezable definitely helps when using databound menus with datatemplates containing
<ribbon:Button Context="MenuItem" Command="X">
  <ribbon:Button.CommandParameter>
    <local:FreezableCheckableCommandParameter Tag="{Binding}" />
[..]
When I use the original CheckableCommandParameter then the bound Tag is always null (in (Can)Execute(d) events).
    public class FreezableCheckableCommandParameter : Freezable, ICheckableCommandParameter
    {
        public static readonly DependencyProperty HandledProperty;
        public static readonly DependencyProperty IsCheckedProperty;
        public static readonly DependencyProperty TagProperty;

        static FreezableCheckableCommandParameter()
        {
            HandledProperty = DependencyProperty.Register("Handled", typeof(bool), typeof(FreezableCheckableCommandParameter));
            IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool?), typeof(FreezableCheckableCommandParameter));
            TagProperty = DependencyProperty.Register("Tag", typeof(object), typeof(FreezableCheckableCommandParameter));
        }

        public bool Handled
        {
            get { return (bool)this.GetValue(HandledProperty); }
            set { this.SetValue(HandledProperty, value); }
        }

        public bool? IsChecked
        {
            get { return (bool?)this.GetValue(IsCheckedProperty); }
            set { this.SetValue(IsCheckedProperty, value); }
        }

        public object Tag
        {
            get { return this.GetValue(TagProperty); }
            set { this.SetValue(TagProperty, value); }
        }

        protected override Freezable CreateInstanceCore()
        {
            throw new NotImplementedException();
        }
    }
Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Markus,

I do see it working when inheriting Freezable with your sample. We'll make this change for the upcoming release.

Actipro Software Support
Information The latest build of this product (2014.1 build 0602) was released 1 month ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.