将Class的不同属性绑定到同一UserControl的两个实例

时间:2017-02-22 01:39:13

标签: c# wpf vb.net xaml user-controls

我创建了一个简单的UserControl,里面有TextBox,我还创建了一个Person类,它有两个属性'FirstName'和'LastName'。

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

所以我在TextBox UserControl中创建了两个同一个Form1的实例,方法是将它们托管在两个单独的ElementHost控件中。

现在我正在尝试将第一个TextBox UserControls TextProperty绑定到我的Person类'FirstName'属性,将另一个TextBox UserControl TextProperty绑定到我的Person类'LastName'属性。我不知道该怎么做。

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

我知道我可以将TextBoxs中的两个添加到一个UserControl StackPanel或类似的内容中,并在xaml中设置绑定,但这不是我想要的。有没有办法将UserControls绑定到其xaml或后面代码之外的其他类?

UserControl代码 - 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

1 个答案:

答案 0 :(得分: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

并将TextBox(TextBoxUC.xaml)中的UserControl绑定到此:

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

然后您可以像往常一样将此依赖项属性绑定到Person DataContext的属性:

<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"))