WPF CustomControl 命令和数据绑定

发布于 2024-08-21 18:20:23 字数 2708 浏览 2 评论 0原文

好吧,我有一个名为 Dialog 的自定义控件来简化问题 这是我的 vb.net 代码:

Public Class Dialog
    Inherits System.Windows.Controls.Control

#Region "DependencyProperties"

    Public Shared ReadOnly OkCommandProperty As DependencyProperty = _
                           DependencyProperty.Register("OkCommand", _
                           GetType(ICommand), GetType(Dialog), _
                           New FrameworkPropertyMetadata(Nothing))

    Private Shared ReadOnly YesCommandPropertyKey As DependencyPropertyKey = _
                            DependencyProperty.RegisterReadOnly("YesCommand", _
                            GetType(ICommand), GetType(Dialog), _
                            New FrameworkPropertyMetadata(Nothing))

    Public Shared ReadOnly YesCommandProperty As DependencyProperty = _
                           YesCommandPropertyKey.DependencyProperty

#End Region

    Public ReadOnly Property YesCommand() As ICommand
        Get
            Return CType(GetValue(ConfirmationDialog.YesCommandProperty), ICommand)
        End Get
    End Property



    Public Sub New()
        MyBase.New()
        SetValue(ConfirmationDialog.YesCommandPropertyKey, New RelayCommand(AddressOf Yes))
    End Sub


#Region "Commands"
    Public Property OkCommand() As ICommand
        Get
            Return CType(GetValue(OkCommandProperty), ICommand)
        End Get
        Set(ByVal value As ICommand)
            SetValue(OkCommandProperty, value)
        End Set
    End Property
#End Region

#Region "Functions"
    Sub Ok()
        Dim command As ICommand = OkCommand
        If (command Is Nothing AndAlso command.CanExecute(Nothing)) Then
            command.Execute(Nothing)
        End If
    End Sub

    Sub Yes(ByVal parameter As Object)
        Ok()
        Me.Visibility = Windows.Visibility.Collapsed
    End Sub
#End Region

    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(Dialog), New FrameworkPropertyMetadata(GetType(Dialog)))
    End Sub
End Class

这是我的 xaml:

<Style TargetType="{x:Type local:Dialog}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Dialog}">
               <Button Content="Yes" Command="{Binding YesCommand}"/> 
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但是 YesCommand 似乎不起作用,但如果我把 OkCommand 放在它的工作原理中,为什么我不能绑定到在代码隐藏中定义和设置的命令(或控制代码) )?

Well i have a custom control called Dialog to slim it down to the problem
Here is my vb.net code:

Public Class Dialog
    Inherits System.Windows.Controls.Control

#Region "DependencyProperties"

    Public Shared ReadOnly OkCommandProperty As DependencyProperty = _
                           DependencyProperty.Register("OkCommand", _
                           GetType(ICommand), GetType(Dialog), _
                           New FrameworkPropertyMetadata(Nothing))

    Private Shared ReadOnly YesCommandPropertyKey As DependencyPropertyKey = _
                            DependencyProperty.RegisterReadOnly("YesCommand", _
                            GetType(ICommand), GetType(Dialog), _
                            New FrameworkPropertyMetadata(Nothing))

    Public Shared ReadOnly YesCommandProperty As DependencyProperty = _
                           YesCommandPropertyKey.DependencyProperty

#End Region

    Public ReadOnly Property YesCommand() As ICommand
        Get
            Return CType(GetValue(ConfirmationDialog.YesCommandProperty), ICommand)
        End Get
    End Property



    Public Sub New()
        MyBase.New()
        SetValue(ConfirmationDialog.YesCommandPropertyKey, New RelayCommand(AddressOf Yes))
    End Sub


#Region "Commands"
    Public Property OkCommand() As ICommand
        Get
            Return CType(GetValue(OkCommandProperty), ICommand)
        End Get
        Set(ByVal value As ICommand)
            SetValue(OkCommandProperty, value)
        End Set
    End Property
#End Region

#Region "Functions"
    Sub Ok()
        Dim command As ICommand = OkCommand
        If (command Is Nothing AndAlso command.CanExecute(Nothing)) Then
            command.Execute(Nothing)
        End If
    End Sub

    Sub Yes(ByVal parameter As Object)
        Ok()
        Me.Visibility = Windows.Visibility.Collapsed
    End Sub
#End Region

    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(Dialog), New FrameworkPropertyMetadata(GetType(Dialog)))
    End Sub
End Class

and here is my xaml:

<Style TargetType="{x:Type local:Dialog}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Dialog}">
               <Button Content="Yes" Command="{Binding YesCommand}"/> 
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

But YesCommand does not seem to work, but if i put OkCommand it works so why can't i bind to a command thats defined and set in codebehind (or the control code)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风苍溪 2024-08-28 18:20:23

你的数据上下文是什么?是否需要设置:

Public Sub New()
    MyBase.New()
    SetValue(ConfirmationDialog.YesCommandPropertyKey, New RelayCommand(AddressOf Yes))
    DataContext = Me
End Sub

What is your DataContext? Do you need to set it:

Public Sub New()
    MyBase.New()
    SetValue(ConfirmationDialog.YesCommandPropertyKey, New RelayCommand(AddressOf Yes))
    DataContext = Me
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文