Framework-Class-Library-Extension
Framework-Class-Library-Extension copied to clipboard
Command binding does not work with a custom dependency property of ICommand type.
When I developed a custom user control that exposes a custom dependency property of ICommand type the exception is shown saying "there is no ID for given dependency property".
[DependencyProperty( Properties.OpenNotificationsCommand)]
public ICommand OpenNotificationsCommand { get; set; }
Maybe it is caused by the aspect [WpfControl( typeof( Properties ) )] which does not allow to expose commands ID's (Property of a command type is not accepted?).
Possible workaround solution is to use a control template and then template bindings. In that case exception is not thrown.
I don't fully understand the error. Could you paste the exact error message you get (the stack, e.g. as such)? Is it one I throw from the DependencyPropertyFactory, if so, which one exactly where? Or is it an error from WPF? When does it get thrown? Upon initialization? When trying to trigger the command?
Control code behind (non-framwork way is working):
[WpfControl( typeof( Properties ) )]
public partial class NotificationButton
{
public enum Properties
{
SomeCommand
}
[DependencyProperty( Properties.SomeCommand )]
public ICommand SomeCommand { get; set; }
public static readonly DependencyProperty OpenNotificationsCommandProperty = DependencyProperty.Register(
"OpenNotificationsCommand", typeof( ICommand ),
typeof( NotificationButton ) );
}
Usage in parent control (ActivityBar):
<common:NotificationButton
OpenNotificationsCommand="{wtc:CommandBinding {x:Static barBinding:Commands.ShowNotifications}}"
SomeCommand="{wtc:CommandBinding {x:Static barBinding:Commands.ShowNotifications}}"/>
XAML usage
<Button
Cursor="Hand"
Style="{Binding ButtonStyle}"
Command="{Binding SomeCommand}">
<Image Source="{Binding ButtonImage}" />
</Button>
Or 2nd option:
<Button
Cursor="Hand"
Style="{Binding ButtonStyle}"
Command="{commandFactory:CommandBinding {x:Static common:NotificationButton.Properties.SomeCommand}}">
<Image Source="{Binding ButtonImage}" />
</Button>
And finally inner exception thrown during compile for 1st option: "No CommandFactory for ID type "Laevo.ViewModel.ActivityBar.Binding.Commands" in type "Laevo.View.Common.NotificationButton" found."
stack trace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
and for 2nd option: "'Laevo.View.Common.NotificationButton.Properties.SomeCommand' StaticExtension value cannot be resolved to an enumeration, static field, or static property."
stack trace: System.Windows.Markup.XamlParseException occurred HResult=-2146233087 Message=Provide value on 'MS.Internal.Markup.StaticExtension' threw an exception. Source=PresentationFramework LineNumber=0 LinePosition=0 StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) InnerException: System.ArgumentException HResult=-2147024809 Message='Laevo.View.Common.NotificationButton.Properties.SomeCommand' StaticExtension value cannot be resolved to an enumeration, static field, or static property. Source=System.Xaml StackTrace: at System.Windows.Markup.StaticExtension.ProvideValue(IServiceProvider serviceProvider) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider) InnerException:
Took 1 hour to describe all that and my visual studio almost died from the amount of exception, hope it helps. :D