Я не понимаю, почему привязка работает для текстового поля, но не работает для пользовательского управления. На изображении ниже вы видите, как это должно работать. Служба может связываться с желтым пользовательским элементом управления, и этот пользовательский элемент управления содержит свойство моего собственного класса. В моем случае это свойство называется Электронная почта. Проблема в том, что этот адрес электронной почты никогда не привязан к желтому элементу управления пользователем. Если я заменю элемент управления пользователем простым элементом управления «TextBox», он будет работать правильно.
Подскажите, пожалуйста, как получить переплетную работу?
Программный код главной страницы Silvelright
#Region "UserProfile"
''' <summary>
''' UserProfile Dependency Property
''' </summary>
Public Shared ReadOnly UserProfileProperty As DependencyProperty = _
DependencyProperty.Register("UserProfile", GetType(ServiceReference1.UserProfile), GetType(MainPage), _
New Windows.PropertyMetadata(Nothing, _
AddressOf OnUserProfileChanged))
''' <summary>
''' Gets or sets the UserProfile property. This dependency property
''' indicates ....
''' </summary>
Public Property UserProfile() As ServiceReference1.UserProfile
Get
Return CType(GetValue(UserProfileProperty), ServiceReference1.UserProfile)
End Get
Set(ByVal value As ServiceReference1.UserProfile)
SetValue(UserProfileProperty, value)
End Set
End Property
''' <summary>
''' Handles changes to the UserProfile property.
''' </summary>
Private Overloads Shared Sub OnUserProfileChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim target As MainPage = CType(d, MainPage)
Dim oldUserProfile As ServiceReference1.UserProfile = CType(e.OldValue, ServiceReference1.UserProfile)
Dim newUserProfile As ServiceReference1.UserProfile = target.UserProfile
target.OnUserProfileChanged(oldUserProfile, newUserProfile)
End Sub
''' <summary>
''' Provides derived classes an opportunity to handle changes to the UserProfile property.
''' </summary>
Protected Overridable Overloads Sub OnUserProfileChanged(ByVal oldUserProfile As ServiceReference1.UserProfile, ByVal newUserProfile As ServiceReference1.UserProfile)
Me.DataContext = newUserProfile
End Sub
#End Region
при отслеживании свойства элемент «newUserProfile» был успешно установлен в выделенном коде.
XAML
<UserControl x:Class="CH_App.ucUserEditor"
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:my="clr-namespace:CH_App"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox Text="{Binding Path=Email}"/>
<my:ucDbRow Title="Email" Value="{Binding Path=Email, Mode=TwoWay}" />
</Grid>
</UserControl>
Texbox с привязкой электронной почты работает так, как должен, и показывает адрес электронной почты. Usercontrol не показывает адрес электронной почты. Пользовательский элемент управления показывает правильный заголовок.
UserControl
<UserControl x:Class="CH_App.ucDbRow"
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:my="clr-namespace:CH_App"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DesignHeight="300" d:DesignWidth="400">
<StackPanel>
<TextBlock x:Name="txtTitle" Text="{Binding Path=Title}" />
<TextBox x:Name="txtValue" Text="{Binding Path=Value, Mode=TwoWay}"/>
</StackPanel>
</UserControl>
Программная поддержка пользовательского управления
#Region "Title"
''' <summary>
''' Title Dependency Property
''' </summary>
Public Shared ReadOnly TitleProperty As DependencyProperty = _
DependencyProperty.Register("Title", GetType(String), GetType(ucDbRow), _
New Windows.PropertyMetadata(""))
''' <summary>
''' Gets or sets the Title property. This dependency property
''' indicates ....
''' </summary>
Public Property Title() As String
Get
Return CType(GetValue(TitleProperty), String)
End Get
Set(ByVal value As String)
SetValue(TitleProperty, value)
End Set
End Property
#End Region
#Region "Value"
''' <summary>
''' Value Dependency Property
''' </summary>
Public Shared ReadOnly ValueProperty As DependencyProperty = _
DependencyProperty.Register("Value", GetType(String), GetType(ucDbRow), _
New Windows.PropertyMetadata(""))
''' <summary>
''' Gets or sets the Value property. This dependency property
''' indicates ....
''' </summary>
Public Property Value() As String
Get
Return CType(GetValue(ValueProperty), Object)
End Get
Set(ByVal value As String)
SetValue(ValueProperty, value)
End Set
End Property
#End Region