2009-12-15 4 views
1

Я создаю пользовательский элемент управления Videoplayer (проект под названием WpfCustomControlLibrary1) и хочу добавить команду Load.wpf command custom control binding xaml

Это то, что я добавил в своем классе, чтобы получить эту команду:

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 
... 

И это, как я называю это из моего 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="..." /> 
..... 

Но когда я добавить UserControl в новый проект, как это:

<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> 

Я получаю сообщение об ошибке, что он не может преобразовать строку в атрибут «Команда» в объект типа «System.Windows.Input.ICommand

Кто-нибудь видит, что происходит не так?

Спасибо за помощь, Нико

ответ

1

у вас есть орфографическая ошибка:-

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

должен быть

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

Я не знаю, если это вызывает ошибку, но, возможно, ,

+0

Да, в этом была проблема: D Большое спасибо! : D – Nico

0

Я думаю, что вы хотите сделать, это объявить LoadCommand как пример, а не общей переменной, так:

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 
... 

Затем связать свойство Button.Command в XAML, так:

<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>