在 ListView 数据模板 WPF 中绑定 UserControl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22595388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:18:52  来源:igfitidea点击:

Binding UserControl inside a ListView datatemplate WPF

wpfxamlbindingdependency-properties

提问by CogentP

So i have this issue:

所以我有这个问题:

  • I have created a UserControl (basically a textbox and label). This user control I am able to Bind using a Dependency Property.
  • I have a ListView in which I can put a textbox in the datatemplate and bind the "text" property to the binded value.
  • 我创建了一个 UserControl(基本上是一个文本框和标签)。这个用户控件我可以使用依赖属性进行绑定。
  • 我有一个 ListView,我可以在其中在数据模板中放置一个文本框并将“text”属性绑定到绑定值。

so far so good. but now if I try to put the UserControl into that very same scenario the DependencyProperty stops working.

到目前为止,一切都很好。但是现在,如果我尝试将 UserControl 放入完全相同的场景中,DependencyProperty 将停止工作。

Here is the code: [ListView]

这是代码:[ListView]

<ListView x:Name="DtContactDetailListView" ItemsSource="{Binding}">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <UserControl:tbx label="{Binding detail}" text="{Binding value}"/>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListView>

The binding in this situation works when its outside the listview, in other parts of the form... so its not an issue with my DepedencyProperty. Also substituting the UserControl for a Textbox, and adding the exact same Binding also works. But it doesn't work under this... why?!

当它在列表视图之外,在表单的其他部分时,这种情况下的绑定工作......所以它不是我的 DepedencyProperty 的问题。同样用 UserControl 代替 Textbox,并添加完全相同的 Binding 也可以。但是在这种情况下不起作用……为什么?!

UPDATE As requested, I have updated the code for the UserControl. Keep in mind this works perfectly when binding it to other elements on the window or page. Just doesn't work inside a listview.

更新 根据要求,我更新了 UserControl 的代码。请记住,这在将其绑定到窗口或页面上的其他元素时非常有效。只是在列表视图中不起作用。

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

回答by Rohit Vats

Like I mentioned in the comment issue is in your UserControl, you have explicitly set DataContextof userControl to itself:

就像我在评论中提到的问题是在您的 UserControl 中,您已经明确地将DataContextuserControl设置为自身:

Me.DataContext = Me

So, binding label="{Binding detail}"looking for property detailin dataContext of UserControl (itself) and not in underlying dataContext of ListBoxItem.

因此,绑定在 UserControl(本身)的 dataContext 中而不是在 ListBoxItem 的基础 dataContext 中label="{Binding detail}"查找属性detail

You have to do explicit binding in case you want to look for item in DataContext of ListBoxItem like:

如果您想在 ListBoxItem 的 DataContext 中查找项目,则必须进行显式绑定,例如:

label="{Binding DataContext.detail,
                RelativeSource={RelativeSource Mode=FindAncestor, 
                                            AncestorType=ListBoxItem}}"

OR

或者

You should remove setting DataContextto itself in UserControl.

您应该DataContext在 UserControl 中删除对自身的设置。

You must have set it to bind with declared DP's. I would suggest to bind that using ElementNameand remove setting DataContext.That way you don't have to give explicit binding and your UserControl will automatically inherit DataContext from its Visual parent.

您必须将其设置为与声明的 DP 绑定。我建议使用ElementName并删除设置 DataContext绑定它。这样您就不必提供显式绑定,您的 UserControl 将自动从其 Visual 父级继承 DataContext。

<UserControl x:Name="myUserControl">
   <Label Content="{Binding label, ElementName=myUserControl}"/>
   <TextBlock Text="{Binding text, ElementName=myUserControl}"/>
</UserControl>