我不明白爲什麼綁定工作的文本框,但不適用於usercontrol。 在下面的圖片中,您會看到它應該如何工作。該服務可以綁定到黃色的用戶控件,並且此用戶控件包含我自己的類的屬性。在我的情況下,這個屬性被稱爲電子郵件。問題是,這電子郵件永遠不會綁定到黃色的用戶控件。 如果我通過簡單的「TextBox」控件替換usercontrol,它就可以正常工作。如何設置datacontext和自定義usercontrol綁定
請你能指教我如何獲得綁定工作?跟蹤屬性時的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 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問題? – NVM 2012-03-16 13:28:59
我正在Silverlight上工作。但上個月,我在WPF上遇到了類似的問題,但是我在那裏沒有綁定。現在我想解決它,因爲我確信,我錯過了一小段聲明。我現在用不同的綁定關鍵字嘗試了幾個小時,但它從來沒有奏效。 – Nasenbaer 2012-03-16 13:33:10
我兩次閱讀這個問題,仍然沒有得到你想要做的。提供你想要首先實現的細節。 – NVM 2012-03-16 13:42:09