用户控件绑定一个ListView WPF的DataTemplate内 [英] Binding UserControl inside a ListView datatemplate WPF

查看:351
本文介绍了用户控件绑定一个ListView WPF的DataTemplate内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个问题:


  • 我创建了一个用户控件(基本上是一个文本框和标签)。这个用户控件我可以用一个依赖属性进行绑定。

  • 我有一个ListView中,我可以把在DataTemplate中的文本框,并绑定文本属性的绑定值。

到目前为止好。但现在,如果我尝试把用户控件到的DependencyProperty停止工作是很相同的情况。

下面是code: [ListView控件]

 < ListView的X:名称=DtContactDetailListView的ItemsSource ={结合}>
   < ListBox.ItemTemplate>
      <&DataTemplate的GT;
         <用户控件:TBX标签={结合详细}文本={绑定值}/>
      < / DataTemplate中>
   < /ListBox.ItemTemplate>
< /&的ListView GT;

在这种情况下,结合工作时,它的列表视图,表单的其他部分......所以它不是我的DepedencyProperty的问题之外。也替换用户控件的文本框,并添加完全相同的绑定也适用。不过,这并不在此工作,为什么......?!

更新
按照要求,我已经更新了code为用户控件。请记住,这是绑定到窗口或页面上的其他元素时,完美的作品。只是没有一个ListView内工作。

 的Public Sub New()
    的InitializeComponent()
    Me.DataContext =我
结束小组文本框属性
公共共享只读tbxTextProperty作为的DependencyProperty = DependencyProperty.Register(文字的GetType([字符串])的GetType(TBX),新FrameworkPropertyMetadata(的String.Empty))
公共属性Text()由于[字符串]
    得到
        返回的GetValue(tbxTextProperty)的ToString()
    到底得的
    设置(值[字符串])
        的SetValue(tbxTextProperty,值)
    结束设定
高端物业


解决方案

就像我在评论问题提到的是在你的用户控件,你已经明确设置用户控件到的DataContext 本身:

  Me.DataContext =我

因此​​,结合标签={结合详细} 寻找财产详细信息中的DataContext用户控件(本身)而不是在ListBoxItem中的潜在的DataContext

您要做明确的情况下,结合你想寻找项目在ListBoxItem中的DataContext的,如:

 标签={结合DataContext.detail,
                的RelativeSource = {的RelativeSource模式= FindAncestor,
                                            AncestorType = ListBoxItem中}}

您应该删除用户控件设置的DataContext 本身。

您必须设置与申报DP的绑定。我建议为绑定,使用的ElementName 并删除设置的DataContext。这样,你没有给予明确的绑定和您的用户控件将自动继承DataContext的从视觉父。

 <用户控件X:名称=的MyUserControl>
   <标签内容={绑定标签的ElementName =}的MyUserControl/>
   < TextBlock的文本={结合文本,的ElementName =}的MyUserControl/>
< /用户控件>

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.

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

Here is the code: [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?!

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.

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

解决方案

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

Me.DataContext = Me

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

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

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

OR

You should remove setting DataContext to itself in UserControl.

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

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

这篇关于用户控件绑定一个ListView WPF的DataTemplate内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆