我正在努力教自己在wpf中创建用户控件的基础知识。为此,我一直在尝试构建数据导航控件,以允许导航各种视图模型检索的记录。我的长期计划是完全自包含的自定义控件,但我想首先掌握较小的点,所以为此我想知道如何制作命令和命令参数属性(以及用户控件本身的用户控件依赖属性的一部分按钮的Is Enabled属性。
我已经成功地制作了整个用户控件的各种按钮依赖属性的各种图像和图像高度和宽度属性,但到目前为止还没有使用Command,Command Parameter和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按钮的属性,现在我想要做的是访问已经存在的那些按钮的属性并从我的viewmodels绑定到它们(通过我的用户控件)
答案 0 :(得分:0)
它与任何其他依赖属性相同。 你像这样声明DP:
<UserControl ...>
<Button Command={Binding ThisCommand} ... />
</UserControl>
并在您的用户控件的XAML中:
object
您以相同的方式设置参数,但类型为UserControl
,您必须在命令处理程序中将其强制转换为正确的类型。
当您使用<local:thisControl ThisCommand={Binding whateverCommandYouWantToBindTo},
ThisCommandParameter={Binding whateverParameterYouWant)>
时,它是这样的:
whateverCommandYouWantToBindTo
除了类型之外,它与其他任何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
也必须设置为ICommand。
人们也可能tell you that defining usercontrols is bad并使用模板,而且在大多数情况下它可能是更好的方法。但是如果你想了解DP,我会说学习。
以下是我在我面前工作的一个例子:
<StackPanel>
<ListBox ... />
<Button
...
Command="{Binding EditButtonCommand}"
CommandParameter="{Binding EditButtonCommandParameter}"/>
</StackPanel>
在UserControl XAML中:
<local:PersonListControl
...
EditButtonCommand="{Binding PersonListEditCommand}"
EditButtonCommandParameter="{Binding Parents}"/>
我像这样使用这个UserControl:
def dates_for_review_select
[Date.today, (Date.today + 2.weeks), (Date.today + 1.month), (Date.today + 2.months), (Date.today + 3.months)]
end