[英]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.