【问题标题】:DataContext of Window cannot be found for DataTemplate找不到 DataTemplate 的 Window 的 DataContext
【发布时间】:2019-10-10 13:35:08
【问题描述】:

我浏览了几篇文章,但似乎没有一篇能解决我的问题。

我正在尝试绑定等待指示器控件的可见性。 我的第一次尝试是这样的

<dx:LoadingDecorator Panel.ZIndex="5"
                     DataContext="{Binding}"
                     SplashScreenDataContext="{Binding}">
    <dx:LoadingDecorator.SplashScreenTemplate>
        <DataTemplate >
            <dx:WaitIndicator DeferedVisibility="{Binding IsActive}" 
                      Content="Loading" />
        </DataTemplate>
    </dx:LoadingDecorator.SplashScreenTemplate>

    <!-- TreeList... -->

</dx:LoadingDecorator>

那没用,根据日志找不到值,因为 DataItem 为空。

Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:IsActive; DataItem=null; target element is 'WaitIndicator' (Name=''); target property is 'DeferedVisibility' (type 'Boolean')

(有趣的是,如果我在选择IsActive 后使用F12,Visual Studio 会跳转到正确视图模型中的正确属性)

之后,我尝试像这样直接引用DataContext

<dx:LoadingDecorator Panel.ZIndex="5"
                     DataContext="{Binding}"
                     SplashScreenDataContext="{Binding}">
    <dx:LoadingDecorator.SplashScreenTemplate>
        <DataTemplate >
            <dx:WaitIndicator DeferedVisibility="{Binding ElementName=ParentWindow, Path=DataContext.IsActive, diag:PresentationTraceSources.TraceLevel=High}" 
                      Content="Loading" />
        </DataTemplate>
    </dx:LoadingDecorator.SplashScreenTemplate>

    <!-- TreeList... -->

</dx:LoadingDecorator>

ParentWindow 是这样定义的

<DXWindow x:Name="ParentWindow" />

DataContext 像往常一样

<dx:DXWindow.DataContext>
    <vm:MainWindowViewModel />
</dx:DXWindow.DataContext>

这也不行,日志显示这个

Created BindingExpression (hash=23511423) for Binding (hash=8575450)
  Path: 'DataContext.IsActive'
BindingExpression (hash=23511423): Default mode resolved to OneWay
BindingExpression (hash=23511423): Default update trigger resolved to PropertyChanged
BindingExpression (hash=23511423): Attach to DevExpress.Xpf.Core.WaitIndicator.DeferedVisibility (hash=24910686)
BindingExpression (hash=23511423): Resolving source 
BindingExpression (hash=23511423): Found data context element: <null> (OK)
    Lookup name ParentWindow:  queried WaitIndicator (hash=24910686)
 BindingExpression (hash=23511423): Resolve source deferred
    Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=DataContext.IsActive; DataItem=null; target element is 'WaitIndicator' (Name=''); target property is 'DeferedVisibility' (type 'Boolean')
 BindingExpression (hash=23511423): Resolving source 
 BindingExpression (hash=23511423): Found data context element: <null> (OK)
     Lookup name ParentWindow:  queried WaitIndicator (hash=24910686)
 BindingExpression (hash=23511423): Resolving source 
 BindingExpression (hash=23511423): Found data context element: <null> (OK)
     Lookup name ParentWindow:  queried WaitIndicator (hash=24910686)
 BindingExpression (hash=23511423): Resolving source  (last chance)
 BindingExpression (hash=23511423): Found data context element: <null> (OK)
     Lookup name ParentWindow:  queried WaitIndicator (hash=24910686)
Cannot find source for binding with reference 'ElementName=ParentWindow'. BindingExpression:Path=DataContext.IsActive; DataItem=null; target element is 'WaitIndicator' (Name=''); target property is 'DeferedVisibility' (type 'Boolean')

这两行我觉得很有趣:

BindingExpression (hash=23511423): Found data context element: <null> (OK)
    Lookup name ParentWindow:  queried WaitIndicator (hash=24910686)

为什么DataContext 为空?我还有什么遗漏的吗?


编辑

在调整来自@Andy 的更改后,该属性已正确绑定,但仍然缺少一些东西。用户界面不更新。我确定INotifyPropertyChanged 事件已正确实施。 日志打印如下:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=1669504) for Binding (hash=32903719)
System.Windows.Data Warning: 58 :   Path: 'IsActive'
System.Windows.Data Warning: 62 : BindingExpression (hash=1669504): Attach to DevExpress.Xpf.Core.WaitIndicator.DeferedVisibility (hash=14696841)
System.Windows.Data Warning: 67 : BindingExpression (hash=1669504): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=1669504): Found data context element: <null> (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=1669504): Activate with root item MainWindowViewModel (hash=42302050)
System.Windows.Data Warning: 108 : BindingExpression (hash=1669504):   At level 0 - for MainWindowViewModel.IsActive found accessor ReflectPropertyDescriptor(IsActive)
System.Windows.Data Warning: 104 : BindingExpression (hash=1669504): Replace item at level 0 with MainWindowViewModel (hash=42302050), using accessor ReflectPropertyDescriptor(IsActive)
System.Windows.Data Warning: 101 : BindingExpression (hash=1669504): GetValue at level 0 from MainWindowViewModel (hash=42302050) using ReflectPropertyDescriptor(IsActive): 'False'
System.Windows.Data Warning: 80 : BindingExpression (hash=1669504): TransferValue - got raw value 'False'
System.Windows.Data Warning: 89 : BindingExpression (hash=1669504): TransferValue - using final value 'False'

到目前为止看起来不错 - 可以找到 Viewmodel。 但是,这条线似乎仍然是一个问题。

System.Windows.Data Warning: 70 : BindingExpression (hash=1669504): Found data context element: <null> (OK)

更新后的 XAML 如下所示

<dx:LoadingDecorator Panel.ZIndex="5"
                                 Grid.Row="1"
                                 Grid.Column="1">
    <dx:LoadingDecorator.SplashScreenTemplate  >
        <DataTemplate >
            <dx:WaitIndicator 
                    DeferedVisibility="{Binding Source={StaticResource ResourceKey=mainVM}, 
                        Path=IsActive, 
                        diag:PresentationTraceSources.TraceLevel=High, 
                        Mode=TwoWay, 
                        NotifyOnSourceUpdated=True, 
                        NotifyOnTargetUpdated=True, 
                        UpdateSourceTrigger=PropertyChanged}"
                    Content="Loading" />
        </DataTemplate>
    </dx:LoadingDecorator.SplashScreenTemplate>

看起来还是有问题。


编辑 2: 我最终直接使用了WaitIndicator,没有DataTemplate。它工作并产生所需的输出。但是,我仍然不明白为什么我最初的方法不起作用。

【问题讨论】:

  • 你能检查一下RelativeSource是否工作? --- DeferedVisibility="{Binding Path=DataContext.IsActive, RelativeSource={RelativeSource AncestorType={x:Type dx:DXWindow}}}"
  • @user1672994 我已经试过了,还是不行。

标签: c# .net wpf binding devexpress


【解决方案1】:

Datacontext 被标记为继承,因此窗口可视树中的任何控件都将继承窗口的 datacontext,即 vm:MainWindowViewModel。

这个控件似乎不在可视化树中,这意味着它找不到数据上下文。

一旦您从窗口资源中移动视图模型并从那里引用它。

<Window ......    

     DataContext="{DynamicResource vm}"
    >
<Window.Resources>
    <local:MainWindowViewmodel x:Key="vm"/>
</Window.Resources>

还是不行。

意味着模板以某种方式应用到窗口之外。在不了解控件是什么以及如何应用模板的情况下,我只能这么说。

【讨论】:

  • 很遗憾这也不起作用,找到的 Datacontext 仍然为空。
  • 无论您的控件是什么,它都不在可视化树中。将 vm 实例移动到窗口资源。
  • 好点,我已经调整了您的更改。你能看看我在帖子中的编辑吗?
  • 看起来您将延迟可见性绑定到整个视图模型。您需要一个 path=yourproperty,在 source= 之前的绑定上。请注意,逗号
  • 我已经有了那个,直接在 deferedvisibility 下。它只是格式有点奇怪。
猜你喜欢
  • 2012-06-29
  • 2011-11-27
  • 2010-12-29
  • 2014-07-30
  • 2013-02-04
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多