【发布时间】: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