views:

835

answers:

2

Hello there,

i'm building a Videoplayer custom control (Project called WpfCustomControlLibrary1) and want to add a Load Command.

This is what i have added in my class to get this Command:

Public Class VideoPlayer
Inherits Control
...
Public Shared ReadOnly LoadCommad As RoutedUICommand
....

Shared Sub New()
    'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
    'This style is defined in Themes\Generic.xaml
    DefaultStyleKeyProperty.OverrideMetadata(GetType(VideoPlayer), New FrameworkPropertyMetadata(GetType(VideoPlayer)))

    LoadCommad = New RoutedUICommand("Load", "Load Video", GetType(VideoPlayer))
    CommandManager.RegisterClassCommandBinding(GetType(VideoPlayer), New CommandBinding(LoadCommad, AddressOf OnLoadExecuted))
End Sub
...

And this is how i call it from my XAML:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary1">
.....
<Button Command="local:VideoPlayer.LoadCommand"
        DockPanel.Dock="Right" Margin="0 5 5 0"
        Width="30" HorizontalAlignment="Left"
        Content="..." />
.....

But when i add this Usercontrol to a new Project like this:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:bibli="clr-namespace:EigeneControllsBibli;assembly=EigeneControllsBibli"
xmlns:uc="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
Title="Window1" Height="442" Width="804">
<Grid>
    <uc:VideoPlayer Source="C:\Users\Public\Videos\Sample Videos\Bear.wmv" Margin="0,106,369,0"></uc:VideoPlayer>
</Grid>

i get an error that he cant convert the string in the Attribute "Command" into an Object of the Type "System.Windows.Input.ICommand

Does somebody see whats going wrong?

Thanks for helping, Nico

+1  A: 

you have a spelling-mistake:

LoadCommad = New RoutedUICommand("Load", "Load Video", GetType(VideoPlayer))

should be

LoadCommand = New RoutedUICommand("Load", "Load Video", GetType(VideoPlayer))

I don't know if this produces the error, but perhaps.

martin
yes, this was the problem :D Thank you very very much! :D
Nico
A: 

I think what you want to do is declare LoadCommand as an instance rather than Shared variable, so:

Public Class VideoPlayer
Inherits Control
...
Private ReadOnly m_LoadCommand As RoutedUICommand
....

Shared Sub New()
    'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
    'This style is defined in Themes\Generic.xaml
    DefaultStyleKeyProperty.OverrideMetadata(GetType(VideoPlayer), New FrameworkPropertyMetadata(GetType(VideoPlayer)))
End Sub

Sub New()
    m_LoadCommand = New RoutedUICommand("Load", "Load Video", GetType(VideoPlayer))
End Sub

Public Property LoadCommand As ICommand
   Get
      Return m_LoadCommand
   End Get
End Property 
...

Then you bind the Button.Command property in the XAML, so:

<StackPanel>
    <uc:VideoPlayer x:Name="myPlayer" Source="C:\Users\Public\Videos\Sample Videos\Bear.wmv" Margin="0,106,369,0"></uc:VideoPlayer>
    <Button Command="{Binding ElementName=myPlayer, Path=LoadCommand"
        Width="30"
        Content="..." />
</StackPanel>
Coder 42