2017-02-22 50 views
1

我创建了一个简单的UserControl,它内部有一个TextBox,并且我还创建了具有两个属性“FirstName”和“LastName”的Person类。将类的不同属性绑定到相同UserControl的两个实例

我想重用UserControl,但是从Person类不同的属性绑定到UserControls TextProperty的每个实例。

所以我创建了两个TextBox UserControlForm1两个实例通过托管他们在两个单独的ElementHost控件。

现在我试图将第一个TextBox UserControls TextProperty绑定到我的Person类的FirstName属性,并将其他TextBox UserControl TextProperty绑定到我的Person类的LastName属性。我不知道该怎么做。

最终结果应该是我的TextBox UserControl的两个实例,一个显示Person类的名字,另一个显示Person类的姓氏。当我更改Person类的FirstName或LastName属性时,更改应通过绑定反映到每个UserControl

我知道我可以StackPanel或添加一些东西两个TextBoxs成一个单一的UserControl这样的,设置绑定了在XAML中,但是这不是我想要的。有没有办法将UserControls与xaml或back代码之外的其他类绑定?

用户控件代码 - TextBoxUC.xaml

<UserControl x:Class="TextBoxUC" 
     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:local="clr-namespace:ClassPropertiesToTextBox" 
     mc:Ignorable="d" 
     d:DesignHeight="30" d:DesignWidth="300"> 
    <Grid> 
     <TextBox x:Name="tbx" /> 
    </Grid> 
</UserControl> 

Person.vb类代码

Public Class Person 
    Implements INotifyPropertyChanged 

    Private _firstname As String 
    Public Property FirstName() As String 
    Get 
     Return _firstname 
    End Get 
    Set(ByVal value As String) 
     _firstname = value 
     NotifyPropertyChanged("FirstName") 
    End Set 
    End Property 

    Private _lastname As String 
    Public Property LastName() As String 
    Get 
     Return _lastname 
    End Get 
    Set(ByVal value As String) 
     _lastname = value 
     NotifyPropertyChanged("LastName") 
    End Set 
    End Property 

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged 
    Private Sub NotifyPropertyChanged(ByVal propertyName As String) 
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) 
    End Sub 
End Class 

Form1.vb的类代码

Public Class Form1 

    'first TextBox UserControl to display Persons FirstName' 
    Dim WithEvents tbxFirstNameUC As TextBoxUC 
    'second TextBox UserControl to display Persons LastName' 
    Dim WithEvents tbxLastNameUC As TextBoxUC 
    'Person class to be used' 
    Dim p As Person 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    p = New Person 
    p.FirstName = "Homer" 
    p.LastName = "Simpson" 


    tbxFirstNameUC = New TextBoxUC 
    'Set DataContext to my Person class instance' 
    tbxFirstNameUC.DataContext = p 
    'Set binding of Person classes FirstName to my first instance TextBox UserControls Text Property' 
    'Cant get TextBoxes TextProperty' 
    tbxFirstNameUC.tbx.SetBinding(tbxFirstNameUC.tbx.Text, New Binding("FirstName", p, p.FirstName)) 
    'host FirstName TextBox UserControl as a child of ElementHost control' 
    hostTbxFN.Child = tbxFirstNameUC 


    tbxLastNameUC = New TextBoxUC 
    'Set DataContext to my Person class instance' 
    tbxLastNameUC.DataContext = p 
    'Set binding of Person classes FirstName to my first instance TextBox UserControls Text Property' 
    'Cant get TextBoxes TextProperty' 
    tbxLastNameUC.tbx.SetBinding(tbxLastNameUC.tbx.Text, New Binding("LastName", p, p.LastName)) 
    'host LastName TextBox UserControl as a child of ElementHost control' 
    hostTbxLN.Child = tbxLastNameUC 
    End Sub 
End Class 

回答

0

你可以串依赖属性添加到UserControl类(TextBoxUC.xaml.vb):

Public Class TextBoxUC 

    Public Property Text() As String 
     Get 
      Return CType(Me.GetValue(TextProperty), Boolean) 
     End Get 
     Set(ByVal value As String) 
      Me.SetValue(TextProperty, value) 
     End Set 
    End Property 

    Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(TextBoxUC), New PropertyMetadata(Nothing)) 
End Class 

而在UserControl(TextBoxUC.xaml)绑定TextBox这一个:

<TextBox x:Name="tbx" Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}" /> 

然后,你可以只是这种依赖属性绑定到PersonDataContext像往常一样的属性:

<Window x:Class="Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:ClassPropertiesToTextBox" 
     mc:Ignorable="d" 
     Title="Window1" Height="300" Width="300"> 
    <Window.DataContext> 
     <local:Person FirstName="Mickey" LastName="Mouse" /> 
    </Window.DataContext> 
    <StackPanel> 
     <local:TextBoxUC Text="{Binding FirstName}" /> 
    </StackPanel> 
</Window> 

编辑:或者你可以以编程的依赖属性绑定:

p = New Person 
p.FirstName = "Homer" 
p.LastName = "Simpson" 

tbxFirstNameUC = New TextBoxUC 
'Set DataContext to my Person class instance' 
tbxFirstNameUC.DataContext = p 
tbxFirstNameUC.SetBinding(TextBoxUC.NameProperty, New Binding("FirstName")) 
+0

你是个人类“姓”结合'文本框UserControl'在WPF应用程序窗口,但我需要创建这个绑定在Windows窗体中,就像在我的例子中,或者我不介意在Form1中传递Person类。vb'作为'Form1'中'TextBoxUC''UserControl'的引用,可能通过构造函数或方法,并在该UserControl后面的代码中创建绑定,但我不知道设置该代码的代码。我正在使用Windows窗体应用程序,而不是WPF应用程序,但我通过'ElementHost'控件集成了'UserControls'。 – C9C

+0

我也尝试过依赖属性,但我不知道如何通过代码连接它,只能通过xaml。无论如何,谢谢 – C9C

+0

我更新了我的答案,举例说明了如何以编程方式绑定到依赖项属性。希望有所帮助。 – mm8

相关问题