在数据模板中获取绑定路径异常

问题描述

我在数据模板中有一个按钮,其命令绑定如下,

<DataTemplate x:Key="ControlSectionChassistemplate">
<controls:ButtonBadged x:Name="btnDeckLightOn" 
                                               x:Uid="/Infrastructure.GlobalizationLibrary/Resources/BarCodeServiceViewPumpOn"
                              Command="{Binding VmChassisControlData.DeckLightControlCommand,Mode=OneWay}"
                                         Margin="10,0"
                                         Style="{StaticResource BaseButtonBadgedStyle}">
</DataTemplate>

此 DataTemplate 被设置为 ContentControl,如下所示,

<ContentControl x:Name="CustomChassisContentControl"
                                        DataContext="{x:Bind viewmodel}"
                                        ContentTemplate="{StaticResource ControlSectionChassistemplate}"
                                        Visibility="{x:Bind viewmodel.SelectedLocation,Mode=OneWay,Converter={StaticResource ParserToTemplateVisibilityConverter},ConverterParameter=Chassis}" />

现在在 viewmodel 构造函数中,我正在创建 VmChassisControlData 对象,如下所示,

public class Someviewmodel
{
public VmBarCodeServiceChassisControlData VmChassisControlData { get; set; }
public Someviewmodel()
{
VmChassisControlData = _lifeTimeScope.Resolve<VmBarCodeServiceChassisControlData>(); //Creates an instance.
}
}

现在在 VmBarCodeServiceChassisControlData 我有 ICommand 定义如下,

public class VmBarCodeServiceChassisControlData : BindableBaseThreadSafe
{
public ICommand DeckLightControlCommand;
 public VmBarCodeServiceChassisControlData()
        {
            DeckLightControlCommand = new RelayCommand<bool>(TurnDeckLightOnorOff);
        }
private async void TurnDeckLightOnorOff(bool obj)
{
//Do something.
}
} 

问题是当我运行代码时,它给了我一个绑定路径错误,如下所示。事实上,ICommand 变量存在于 VmBarCodeServiceChassisControlData 类中。但是不知道为什么会报错请帮忙

错误:BindingExpression 路径错误:'DeckLightControlCommand' 找不到属性 'Application.viewmodel.Model.VmBarCodeServiceChassisControlData'。 BindingExpression: Path='VmChassisControlData.DeckLightControlCommand' DataItem='Application.viewmodel.BarCodeServiceviewmodel';目标 元素是“Presentation.Common.Controls.ButtonBadged” (Name='btnDeckLightOn');目标属性是“命令”(类型 'ICommand')

仅供参考,在同一个类 VmBarCodeServiceChassisControlData 中,我还有一个其他属性,如下所示,它绑定到组合框并且显示正常。

 public List<EnumLocalizer<HaloColors>> HaloColors
        {
            get
            {
                var HaloColorTypes = new List<EnumLocalizer<HaloColors>>();
                Enum.GetValues(typeof(HaloColors)).Cast<HaloColors>().ToList().ForEach(
                    dataType => HaloColorTypes.Add(new EnumLocalizer<HaloColors>() { Value = dataType }));
                return HaloColorTypes;
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...