界面上有个DataGrid,xaml如下
1 <Window x:Class="WTest.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WTest" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 <Window.Resources>10 <Style TargetType="DataGridCell">11 <Setter Property="Foreground" Value="{Binding StatusColor}" />12 </Style>13 </Window.Resources>14 <Grid>15 <Grid.RowDefinitions>16 <RowDefinition Height="*"></RowDefinition>17 <RowDefinition Height="30"></RowDefinition>18 </Grid.RowDefinitions>19 <DataGrid ItemsSource="{Binding Collects}">20 <DataGrid.Columns>21 <DataGridTextColumn Binding="{Binding Id}" Header="Id"/>22 </DataGrid.Columns>23 </DataGrid>24 <StackPanel Grid.Row="1" Orientation="Horizontal">25 <Button Content="获取数据" Command="{Binding GetData}" HorizontalAlignment="Left" Height="30" Width="75"/>26 <Button Content="修改第一条" Command="{Binding ChangeFirst}" HorizontalAlignment="Left" Height="30" Width="75"/>27 </StackPanel>28 </Grid>29 </Window>
viewmodel如下:
问题是这样的,如果TestModel内部默认的StatusColor不是null,第14行直接add就报错,如果是null,直接add就不报错,不清楚什么原因。那位大佬清楚解释下。
1 public class MainViewModel : BaseNotifyEntity 2 { 3 private ObservableCollection<TestModel> collects = new ObservableCollection<TestModel>(); 4 public ObservableCollection<TestModel> Collects { get => collects; set => NotifyPropertyChanged(nameof(Collects), ref collects, value); } 5 6 public DelegateCommand GetData => new DelegateCommand(async (o) => 7 { 8 var data = await Datas(); 9 int i = 0; 10 data.ForEach((item) => 11 { 12 //item.StatusColor如果为null那就可以直接add 13 //item.StatusColor不为null的时候这里为什么一定要重设颜色才不报错?,否则直接add就报错了 14 item.StatusColor = i % 2 == 0 ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Purple); 15 16 Collects.Add(item); 17 i++; 18 }); 19 }); 20 21 public async Task<List<TestModel>> Datas() 22 { 23 return await Task.Run(async () => 24 { 25 await Task.Delay(0); 26 List<TestModel> test = new List<TestModel>(); 27 Parallel.For(0, 10, (i) => 28 { 29 TestModel t = new TestModel() 30 { 31 Id = i, 32 }; 33 test.Add(t); 34 }); 35 return test; 36 }); 37 } 38 public DelegateCommand ChangeFirst => new DelegateCommand((o) => 39 { 40 if (Collects.Count > 0) 41 { 42 Collects[0].StatusColor = new SolidColorBrush(Colors.Pink); 43 } 44 }); 45 } 46 public class TestModel : BaseNotifyEntity//, ICloneable 47 { 48 public TestModel() 49 { 50 this.StatusColor = new SolidColorBrush(Colors.Red); 51 } 52 private int id; 53 public int Id { get => id; set => NotifyPropertyChanged(nameof(Id), ref id, value); } 54 55 private Brush statusColor; 56 public Brush StatusColor { get => statusColor; set => NotifyPropertyChanged(nameof(StatusColor), ref statusColor, value); } 57 58 //public object Clone() 59 //{ 60 // object obj = null; 61 62 // obj = new TestModel() 63 // { 64 // Id = this.Id, 65 // StatusColor = this.StatusColor 66 // }; 67 // return obj; 68 //} 69 } 70 public class BaseNotifyEntity : INotifyPropertyChanged 71 { 72 public event PropertyChangedEventHandler PropertyChanged; 73 public void NotifyPropertyChanged(string propertyName) 74 { 75 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 76 } 77 protected void NotifyPropertyChanged<T>(string propertyName, ref T field, T newValue = default) 78 { 79 field = newValue; 80 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 81 } 82 } 83 /// <summary> 84 /// 实现DelegateCommand 85 /// </summary> 86 public class DelegateCommand : ICommand 87 { 88 /// <summary> 89 /// 命令所需执行的事件 90 /// </summary> 91 public Action<object> ExecuteCommand { get; set; } 92 93 public DelegateCommand() 94 { 95 } 96 public DelegateCommand(Action<object> execute) 97 { 98 ExecuteCommand = execute; 99 }100 101 /// <summary>102 /// 命令可用性获取103 /// </summary>104 /// <param name="parameter"></param>105 /// <returns></returns>106 public bool CanExecute(object parameter)107 {108 return true;109 }110 111 public event EventHandler CanExecuteChanged112 {113 add { CommandManager.RequerySuggested += value; }114 remove { CommandManager.RequerySuggested -= value; }115 }116 117 /// <summary>118 /// 命令具体执行119 /// </summary>120 /// <param name="parameter"></param>121 public void Execute(object parameter)122 {123 ExecuteCommand(parameter);124 }125 }
源码下载:下载