可从UserControl MVVM外部访问的事件和属性

时间:2014-03-12 19:13:44

标签: wpf mvvm properties user-controls

所以,我有一个名为ApplicationForm的UserControl。它有一些TextBoxes和一个Button。每个TextBox都有一个Binding。一个例子:

<xctk:WatermarkTextBox Watermark="PACAPIME" Text="{Binding NumPac}" Grid.Column="2" Grid.Row="2" />

然后,我的View被绑定到这样的ViewModel:

<UserControl x:Class="Verkooporder"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
             xmlns:vm="clr-namespace:Koala" mc:Ignorable="d" d:DesignHeight="720" d:DesignWidth="1100">
<UserControl.Resources>
    <vm:VerkooporderViewModel x:Key="VerkooporderViewModelDataSource" 
                                      d:IsDataSource="True"/>
</UserControl.Resources>
<UserControl.DataContext>
    <Binding Source="{StaticResource VerkooporderViewModelDataSource}"/>
</UserControl.DataContext>

我的ViewModel中的相应属性是这样的:

Private _oNumPac As String = ""
Public Property NumPac As String
    Get
        Return _oNumPac
    End Get
    Set(ByVal value As String)
        _oNumPac = value
        RaisePropertyChanged()
    End Set
End Property

这在我的UserControl中运行良好。现在,我在MainWindow中调用我的UserControl:

<local:Verkooporder />

我希望能够像NumPac一样访问属性<local:Verkooporder NumPac="" />,但这不起作用。

另外,我如何才能访问Button Command

Public Shared _oNumPac As DependencyProperty = DependencyProperty.Register("NumPac", 
    GetType(String), GetType(VerkooporderViewModel), New PropertyMetadata(""))

Public Property NumPac As String
    Get
        Return GetValue(_oNumPac)
    End Get
    Set(value As String)
        SetValue(_oNumPac, value)
    End Set
End Property 

但是当我尝试输入时它没有出现:

<local:Verkooporder NumPac="Test" />

请注意,属性位于UserControl的ViewModel中,而不是View后面的代码

0 个答案:

没有答案