簡體   English   中英

wpf命令自定義控件綁定xaml

[英]wpf command custom control binding xaml

我正在構建一個Videoplayer自定義控件(名為WpfCustomControlLibrary1的項目),並想添加一個加載命令。

這是我在課堂上添加的以獲得此命令的內容:

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>

我收到一個錯誤,他無法將“ Command”屬性中的字符串轉換為“ System.Windows.Input.ICommand”類型的對象

有人看到出什么問題了嗎?

感謝您的幫助,尼科

您有一個拼寫錯誤:

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

應該

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

我不知道這是否會產生錯誤,但是也許。

我認為您想要做的是將LoadCommand聲明為實例而不是Shared變量,因此:

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

然后在XAML中綁定Button.Command屬性,因此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM