我需要从 Xaml 文件传递一个类对象

本文关键字:一个 对象 Xaml 文件 | 更新日期: 2023-09-27 18:35:05

我正在暗示一个具有两个变量的类,并在xaml中初始化它并将其对象传递给ModelView,然后发布这个对象。

当我这样做时,我正在,在这一行得到一个例外

modelproperty:ModelProperty bMode="enums:Modes.TType" dStyle="enums:StyleModes.PType"/>

例外

"{"'在 "System.Windows.Baml2006.TypeConverterMarkupExtension"抛出了一个 例外。行号'217'和行位置'20'。

Xaml 文件

 <dxb:BarButtonItem x:Name="PointsItem" Content="Points" RibbonStyle="Large"  Command="{Binding StyleCommand}" 
 <dxb:BarButtonItem.CommandParameter>
<modelproperty:ModelProperty bMode="enums:Modes.TType" dStyle="enums:StyleModes.PType" />
</dxb:BarButtonItem.CommandParameter>
</dxb:BarButtonItem>

Cs 文件

public class ModelProperty
    {
        public Modes bMode { get; set; }
        public StyleModes dStyle { get; set; }
        public ModelProperty(Modes _bMode, StyleModes _dStyle)
        {
            bMode = _bMode;
            dStyle = _dStyle;
        }  
        public ModelProperty()
        {
            //To be implemented
        }
    }

查看模型

 this.DrawStyleCommand = new DelegateCommand<ModelProperty>(param =>
            {
                var xModelProperty = param as ModelProperty;
                eventAggregator.GetEvent<StyleChangedEvent>().Publish(xModelProperty);
            });

提前谢谢。

我需要从 Xaml 文件传递一个类对象

<modelproperty:ModelProperty bMode="enums:Modes.TType" dStyle="enums:StyleModes.PType" />

如果您在 UIElement/Control 上执行此操作,这通常有效,其中类型转换器会将字符串转换为相应的类型。由于您是在普通类上执行此操作的,因此它会"enums:Modes.TType"视为字符串而不是值Modes.TType Enum。这就是您获得例外的原因。

您应该考虑在后面的代码中执行此操作,因为您只是要初始化此对象。你可以在构造函数后面的代码中很好地做到这一点。但是,如果您仍然坚持在 XAML 中执行此操作,请尝试使用 TypeConverter