【问题标题】:wpf command custom control binding xamlwpf 命令自定义控件绑定 xaml
【发布时间】:2010-12-26 17:34:45
【问题描述】:

我正在构建一个 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>

我得到一个错误,他无法将属性“命令”中的字符串转换为“System.Windows.Input.ICommand”类型的对象

有人知道出了什么问题吗?

感谢您的帮助, 妮可

【问题讨论】:

    标签: wpf vb.net xaml command custom-controls


    【解决方案1】:

    你有一个拼写错误:

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

    应该是

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

    我不知道这是否会产生错误,但也许。

    【讨论】:

      【解决方案2】:

      我认为你想要做的是将 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 
      ...
      

      然后在 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-29
        • 1970-01-01
        • 2015-06-03
        • 2013-03-15
        • 1970-01-01
        • 2012-04-15
        • 1970-01-01
        • 2011-08-06
        相关资源
        最近更新 更多