WPF中因为大意出现的奇葩问题

1.界面绑定了DataContex,按钮也可以点击,但是跑不到ViewModel中的命令

解答:Locator注册中分两步:第一注册,第二定义实例化ViewModel变量。以上问题忘记了第二步,其实DataContex绑定的是实例化对象而非类名。

2.给集合的某个字段赋值,赋不上去

解答:定义的变量因为复制来的,get对应的变量没替换

3.Page中用new Uri(UriEditWnd, UriKind.RelativeOrAbsolute);显示一个UserControl,竟在Page.cs的构造函数中死循环。

解答:UriEditWnd的相对地址写成了Page的

4.不能将值 NULL 插入列 'ID' 

 解答:表设计时主键未设置标识

5.使用MapperUtils.MapList时注意变量可读可写(GET ;SET)

这样获取枚举名的方法,会导致MapperUtils.MapList出错
  [Chloe.Annotations.NotMapped]
        public string TaskLevelName
        {
            get => MyDescriptionAttribute.GetDisplayName(_taskType);
        }

应该如下书写


private PriceOfCommonditiesType _typeid;
        /// <summary>
        /// TypeID
        /// </summary>	
        public PriceOfCommonditiesType TypeID
        {
            get => _typeid;
            set {
                SetProperty(ref _typeid, value);
                TypeName = MyDescriptionAttribute.GetDisplayName(_typeid);
//不能用_typeName } } private string _typeName; /// <summary> /// 物价类型名称 /// </summary> [Chloe.Annotations.NotMapped] public string TypeName { get => _typeName; set => SetProperty(ref _typeName, value); }

 6.枚举类数据源,以及反向绑定界面:

    注意:xaml和cs数据源中的 selectValuePath需要与selectValue一致。否则发反向无法赋值

xaml中
<ComboBox Grid.Row="1" Grid.Column="1" x:Name="cmb"
                                      ItemsSource="{Binding OcType }"
                                      DisplayMemberPath="DisplayName"
                                      SelectedValuePath="EnumValue" 
                                      SelectedValue="{Binding EditEntity.TypeID,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                      IsEnabled="{Binding EditUIEditable}" >
                            </ComboBox>
cs中
 OcType = new ObservableCollection<Vw_BD_EnumItems<PriceOfCommonditiesType>>();
            var result = Core.EnumsUtils.GetEnumItems<PriceOfCommonditiesType>();
            foreach (var key1 in result.Keys)
            {
                OcType.Add(new Vw_BD_EnumItems<PriceOfCommonditiesType> { EnumValue = key1, DisplayName = result[key1] });
            };

 7.错误 CS0542 “WndFlowMultBack”: 成员名不能与它们的封闭类型相同 

 解:窗体的x:Name不能和窗体的类名一样

原文地址:https://www.cnblogs.com/mamaxiaoling/p/14558285.html