c# - 找不到 DataTemplate 的 Window 的 DataContext
问题描述
我浏览了几篇文章,但似乎没有一篇能解决我的问题。
我正在尝试绑定等待指示器控件的可见性。我的第一次尝试是这个
<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')
(有趣的是,如果我F12
在选择后使用IsActive
,Visual Studio 会跳转到正确 Viewmodel 中的正确属性)
之后,我尝试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>
whereParentWindow
是这样定义的
<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
. 它工作并产生所需的输出。但是,我仍然不明白为什么我的初始方法不起作用。
解决方案
Datacontext 被标记为继承,因此窗口可视树中的任何控件都将继承窗口的 datacontext,即 vm:MainWindowViewModel。
似乎此控件不在可视树中,这意味着它找不到数据上下文。
从窗口资源中移动视图模型并从那里引用它。
<Window ......
DataContext="{DynamicResource vm}"
>
<Window.Resources>
<local:MainWindowViewmodel x:Key="vm"/>
</Window.Resources>
那仍然没有用。
这意味着模板以某种方式应用于窗口之外。在不了解控件是什么以及如何应用模板的情况下,我只能说这些。
推荐阅读
- python - 如何在不更改评分 url 的情况下更新部署的 azure ml Web 服务中的 score.py 文件
- c# - 将 HEX 转换为 UTF-16(GUID 分区名称) - WPF 应用程序(.NET Framework)
- java - java中的代数程序无法正常工作
- android - 从协程更改布局
- python - Plotly:如何在注释中同时使用美元符号和换行符?
- ios - 你能帮我在 UIButton 中添加乐天吗?
- regex - 如何进入带有空格字符的词素的句子?
- android - 将对象添加到可变实时数据列表
- java - 存储在 mysql 数据库中的双值更改
- julia - Julia:在 Pluto 中获取运行时输入