【发布时间】:2012-04-01 22:25:55
【问题描述】:
我不明白为什么绑定适用于文本框但不适用于用户控件。 在下图中,您可以看到它应该如何工作。该服务可以绑定到黄色用户控件,并且此用户控件包含我自己的类的属性。在我的例子中,这个属性被称为 Email。问题是,这个 Email 永远不会绑定到黄色的用户控件。 如果我用简单的“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 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
【问题讨论】:
-
这是 WPF 问题还是 Silverlight 问题?
-
我正在研究 Silverlight。但是上个月我在 WPF 上遇到了类似的问题,但是我做了一个没有绑定的解决方法。现在我想修复它,因为我确定,我只是错过了一小部分声明。我现在用不同的绑定关键字尝试了几个小时,但它从来没有奏效。
-
我读了两遍这个问题,但仍然没有得到你想要做什么。提供您希望首先实现的目标的详细信息。
-
感谢您的建议。我将通过图像更新问题。
-
你为什么不喜欢模板,它们是显而易见的方式?
标签: wpf silverlight xaml binding user-controls