繁体   English   中英

WPF中的“使用缓存的访问器在0级...”数据绑定警告

[英]“At level 0 using cached accessor for…” databinding warning in WPF

我正在调试某人的WPF UserControl(简称为MyUserControl),它基本上只有一个TextBlock和一个Button

MyUserControl出现在Menu ,即MenuItem的ItemTemplate将使用它来显示其项目:

<MenuItem.ItemTemplate>
    <DataTemplate>
        <MenuItem>
            <MenuItem.Header>
                <controls:MyUserControl />
            </MenuItem.Header>                              
        </MenuItem>
    </DataTemplate>
</MenuItem.ItemTemplate>

MyUserControl的XAML如下所示:

<UserControl x:Class="MyUserControl"
             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:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
             mc:Ignorable="d"              
             d:DesignHeight="80" d:DesignWidth="300">
    <StackPanel Orientation="Vertical">
        <!-- date and time at the top of the menu item -->
        <TextBlock Text="{Binding Path=Time, StringFormat={}{0:t}}" Foreground="Black"/>
        <!-- details button -->
        <Button Content="Click Me" Command="{Binding MyCommand, diag:PresentationTraceSources.TraceLevel=High}"/>
    </StackPanel>
</UserControl>

菜单项数据绑定到ObservableCollection<MyViewModel> ,其中MyViewModel仅具有几个公共属性,一个名为Time的DateTime属性和一个名为MyCommand的RelayCommand

public class MyViewModel : GalaSoft.MvvmLight.ViewModelBase
{
    public MyViewModel ()
    {
        Time = DateTime.Now;
        MyCommand = new RelayCommand( ExecuteMyCommand);
    }

    private void ExecuteMyCommand()
    {
        MessageBox.Show( "HI");
    }

    public DateTime Time { get; set; }
    public RelayCommand MyCommand { get; set; }
}

像这样简单地测试MyCommand是临时实现的。

问题是Time数据绑定可以正常工作,而RelayCommand绑定不能正常工作。 当我在ExecuteMyCommand内放置一个断点并单击按钮时,它不会被命中。

在此处输入图片说明

另外,当我单击按钮时,在输出窗口中也会收到以下警告:

System.Windows.Data警告:107:BindingExpression(哈希= 9995714):在级别0使用MyViewModel.MyCommand的缓存访问器:RuntimePropertyInfo(MyCommand)

听起来像是因为这是一个警告,它可能并不表示为什么按钮单击处理程序未执行,所以我还能尝试其他方法吗?

以下是诊断跟踪的输出窗口中的内容:

System.Windows.Data Warning: 67 : BindingExpression (hash=16825): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=16825): Found data context element: Button (hash=622526) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=16825): Activate with root item MyViewModel (hash=59763145)
System.Windows.Data Warning: 107 : BindingExpression (hash=16825):   At level 0 using cached accessor for MyViewModel.MyCommand: RuntimePropertyInfo(MyCommand)
System.Windows.Data Warning: 104 : BindingExpression (hash=16825): Replace item at level 0 with MyViewModel (hash=59763145), using accessor RuntimePropertyInfo(MyCommand)
System.Windows.Data Warning: 101 : BindingExpression (hash=16825): GetValue at level 0 from MyViewModel (hash=59763145) using RuntimePropertyInfo(MyCommand): RelayCommand (hash=35966181)
System.Windows.Data Warning: 80 : BindingExpression (hash=16825): TransferValue - got raw value RelayCommand (hash=35966181)
System.Windows.Data Warning: 89 : BindingExpression (hash=16825): TransferValue - using final value RelayCommand (hash=35966181)

毕竟,也许这是MvvmLightToolkit的RelayCommand中的东西,而不是实际的数据绑定错误?

更新

我还尝试了以下方法,该方法是删除DataTemplate ,放弃使用多余的按钮,而是将ItemContainerStyle应用于MenuItem,以便在单击时间时执行命令处理程序:

<MenuItem.ItemContainerStyle>
    <Style TargetType="MenuItem">
        <Setter Property="Command" Value="{Binding MyCommand}" />
        <Setter Property="Header" Value="{Binding Time}" />
    </Style>
</MenuItem.ItemContainerStyle>

同样,虽然“时间”有效,但该命令无效。

ItemTemplate 应该包含一个MenuItem ,在已为周围的内容容器中创建DataTemplate 点击处理混乱。 绑定可能工作得很好。

MenuItem有一个附加按钮也可能会引起问题(单击可能由MenuItem处理,并且永远不会到达该按钮)。 通常, MenuItems应该只包含文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM