微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在数据模板中获取绑定路径异常

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

<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 举报,一经查实,本站将立刻删除。