2009-12-15 188 views
1

我正在構建一個Videoplayer自定義控件(項目名爲WpfCustomControlLibrary1)並且想要添加一個加載命令。wpf命令自定義控件綁定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="..." /> 
..... 

但當我這個用戶控件添加到一個新的項目是這樣的:

<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 2009-12-15 09:03:17

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>