我有一个问题:
- 我创建了一个用户控件(基本上是文本框和标签)。我能够使用依赖属性进行绑定。
- 我有一个 ListView,在其中可以在 datatemplate 中放置文本框并将 "text" 属性绑定到绑定的值。
到目前为止还不错。但是,如果我尝试将用户控件放到同样的情境中,依赖属性就停止工作了。
这是代码:[ListView]
<ListView x:Name="DtContactDetailListView" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<UserControl:tbx label="{Binding detail}" text="{Binding value}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListView>
在这种情况下,当它在listview之外的其他部分中使用时,绑定是有效的...所以这不是我的DepedencyProperty的问题。同时,用一个Textbox替换UserControl,并添加完全相同的绑定也可以工作。但是在这种情况下却不能工作,为什么?!
更新 按照请求,我已经更新了UserControl的代码。请记住,在将其绑定到窗口或页面上的其他元素时,它可以完美地工作。只是在listview中不起作用。
Public Sub New()
InitializeComponent()
Me.DataContext = Me
End Sub
'TextBox property
Public Shared ReadOnly tbxTextProperty As DependencyProperty = DependencyProperty.Register("text", GetType([String]), GetType(tbx), New FrameworkPropertyMetadata(String.Empty))
Public Property text() As [String]
Get
Return GetValue(tbxTextProperty).ToString()
End Get
Set(value As [String])
SetValue(tbxTextProperty, value)
End Set
End Property
UserControl:tbx
是什么?也许是tbx:UserControl
? - Hamlet Hakobyan