相关疑难解决方法(0)

WPF绑定自己

我有一个WPF Window,在某个地方有一个ListView我绑定List<string>到的地方.

现在在什么地方我ListView有一个TextBoxContent属性设置为{Binding}.

但这是速记.如何编写完整绑定以绑定到自身?

{Binding Path=Self}不起作用,也不起作用{Binding Self}(后者是前者的捷径).

data-binding wpf binding self

80
推荐指数
1
解决办法
8万
查看次数

在UserControl中设置DataContext会影响父级中的绑定

我有一个基本的UserControl设置它DataContext自己以便于绑定:

<UserControl x:Class="MyControlLib.ChildControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             DataContext="{Binding RelativeSource={RelativeSource Self}}">

</UserControl>
Run Code Online (Sandbox Code Playgroud)

这在父XAML文件中使用,如下所示:

<UserControl x:Class="MyControlLib.ParentControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:ctrl="clr-namespace:MyControlLib">

             <ctrl:ChildControl x:Name="ChildName" 
                                PropertyOnChild="{Binding PropertyInParentContext}"/>             
</UserControl>
Run Code Online (Sandbox Code Playgroud)

由于某种原因,这给出了一个绑定错误,似乎表明DataContext父控件的设置受其自身的子控件设置的影响DataContext.

System.Windows.Data错误:40:BindingExpression路径错误:'object'''ChildControl'(Name ='ChildName')'上找不到'PropertyInParentContext'属性.BindingExpression:路径= PropertyInParentContext; DataItem ='ChildControl'(Name ='ChildName'); target元素是'ChildControl'(Name ='ChildName'); target属性是'PropertyOnChild'(输入'whatever')

为什么"PropertyInParentContext"在子控件中而不是在父控件中查找DataContext

如果我删除了

DataContext="{Binding RelativeSource={RelativeSource Self}}
Run Code Online (Sandbox Code Playgroud)

从孩子控制,然后事情运作我期望.

我错过了一些明显的东西吗?

data-binding wpf datacontext xaml user-controls

13
推荐指数
2
解决办法
8713
查看次数

标签 统计

data-binding ×2

wpf ×2

binding ×1

datacontext ×1

self ×1

user-controls ×1

xaml ×1