【问题标题】:How to make the command property of a wpf button a dependency property of a user control如何使 wpf 按钮的命令属性成为用户控件的依赖属性
【发布时间】:2025-12-26 03:40:09
【问题描述】:

我正在尝试自学在 wpf 中创建用户控件的基础知识。为此,我一直在尝试构建一个数据导航控件,以允许通过各种视图模型检索的记录进行导航。我的长期计划是一个完全独立的自定义控件,但我想先掌握较小的点,为此我想知道如何制作命令和命令参数属性(以及Is Enabled 属性)构成我的用户控件本身的用户控件依赖项属性的一部分的按钮。

我已经成功地使整个用户控件的各种按钮依赖属性的各种图像和图像高度和宽度属性,但到目前为止还没有成功地使用命令、命令参数和启用属性。

我欢迎任何人提出任何建议。

我已经有以下内容(我为我的用户控件中的每个按钮设置了):

#Region "Next Button"
Public Property ImageNext() As ImageSource
    Get
        Return DirectCast(GetValue(ImageNextProperty), ImageSource)
    End Get
    Set(value As ImageSource)
        SetValue(ImageNextProperty, value)
    End Set
End Property


Public Shared ReadOnly ImageNextProperty As DependencyProperty = DependencyProperty.Register("ImageNext", GetType(ImageSource), GetType(DataNavigator), New UIPropertyMetadata(Nothing))

Public Property ImageNextWidth() As Double
    Get
        Return CDbl(GetValue(ImageNextWidthProperty))
    End Get
    Set(value As Double)
        SetValue(ImageNextWidthProperty, value)
    End Set
End Property


Public Shared ReadOnly ImageNextWidthProperty As DependencyProperty = DependencyProperty.Register("ImageNextWidth", GetType(Double), GetType(DataNavigator), New UIPropertyMetadata(16.0))

Public Property ImageNextHeight() As Double
    Get
        Return CDbl(GetValue(ImageNextHeightProperty))
    End Get
    Set(value As Double)
        SetValue(ImageNextHeightProperty, value)
    End Set
End Property


    Public Shared ReadOnly ImageNextHeightProperty As DependencyProperty = DependencyProperty.Register("ImageNextHeight", GetType(Double), GetType(DataNavigator), New UIPropertyMetadata(16.0))

然而,这一直在向标准 wpf 按钮添加属性,现在我想做的是访问那些已经存在的按钮的属性并从我的视图模型中绑定到它们(通过我的用户控件)

【问题讨论】:

    标签: wpf vb.net user-controls


    【解决方案1】:

    它与任何其他依赖属性相同。 你这样声明 DP:

    Public Shared ReadOnly ThisCommandProperty As DependencyProperty = _
        DependencyProperty.Register("ThisCommand", GetType(ICommand), _
               GetType(thiscontrol), Nothing)
    
    Public Property ThisCommand As ICommand
        Get
            Return CType(GetValue(ThisCommandProperty), ICommand)
        End Get
        Set(ByVal value As ICommand)
            SetValue(ThisCommandProperty, value)
        End Set
    End Property
    

    在您的用户控件的 XAML 中:

    <UserControl ...> 
        <Button Command={Binding ThisCommand} ... />
    </UserControl>
    

    您以相同的方式设置参数,但类型为object,并且您必须将其转换为命令处理程序中的正确类型。

    当你使用UserControl时,是这样的:

    <local:thisControl ThisCommand={Binding whateverCommandYouWantToBindTo}, 
        ThisCommandParameter={Binding whateverParameterYouWant)>
    

    除了类型之外,它实际上与任何其他 DP 相同。当然,whateverCommandYouWantToBindTo 也必须设置为 ICommand。

    人们也可以tell you that defining usercontrols is bad 并改用模板,这在大多数情况下可能是更好的方法。但如果你想了解 DP,我说学习。

    这是我面前的一个例子:

    Public Shared ReadOnly EditButtonCommandProperty As DependencyProperty = _
        DependencyProperty.Register("EditButtonCommand", _
              GetType(ICommand), GetType(PersonListControl), Nothing)
    
    Public Property EditButtonCommand As ICommand
        Get
            Return CType(GetValue(EditButtonCommandProperty), ICommand)
        End Get
        Set(ByVal value As ICommand)
            SetValue(EditButtonCommandProperty, value)
        End Set
    End Property
    
    Public Shared ReadOnly EditButtonCommandParameterProperty As DependencyProperty = _
        DependencyProperty.Register("EditButtonCommandParameter", GetType(Object), _
                GetType(PersonListControl), Nothing);
    
    Public Property EditButtonCommandParameter As Object
        Get
            Return CType(GetValue(EditButtonCommandParameterProperty), Object)
        End Get
        Set(ByVal value As Object)
            SetValue(EditButtonCommandParameterProperty, value)
        End Set
    End Property
    

    在 UserControl XAML 中:

    <StackPanel>
        <ListBox ... />
        <Button 
            ...
            Command="{Binding EditButtonCommand}"
            CommandParameter="{Binding EditButtonCommandParameter}"/>
    </StackPanel>
    

    我像这样使用这个 UserControl:

    <local:PersonListControl
        ...
        EditButtonCommand="{Binding PersonListEditCommand}"
        EditButtonCommandParameter="{Binding Parents}"/>
    

    【讨论】:

    • 内森,编辑了很多!翻译成 VB 并更正我的语法。非常感谢:-)
    • 我看到同样的问题也在这里得到了回答:*.com/questions/2777452/…
    最近更新 更多