无法在对象'System.Windows.Media.GeometryDrawing'上设置属性,因为它处于只读状态

时间:2019-07-03 02:00:27

标签: c# wpf mvvm

ResourceDictionary:

<DrawingBrush x:Key="SettingIcon" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="{StaticResource IconsBrush}" Geometry="M 28.0875,37.8214C 27.9361,36.9173 28.4282,34.7323 28.7688,33.8659C 30.1316,30.3626 33.1977,28.4414 36.8695,27.9893C 39.2922,27.6503 41.9041,28.9311 43.7211,30.4002C 45.5759,31.9071 46.4465,34.3933 46.7494,36.6913C 47.0522,39.1021 45.803,41.7014 44.2889,43.472C 42.5854,45.4685 40.4656,46.1466 37.9673,46.5233C 35.5447,46.8247 32.9327,45.5815 31.1536,44.0747C 29.2988,42.4925 28.466,40.1946 28.0875,37.8214 Z M 35.6204,-3.05176e-005L 39.1029,-3.05176e-005C 39.6707,0.0376587 40.1628,0.0753174 40.3521,0.150665C 42.1691,0.791077 42.3962,1.84586 42.6612,3.54102C 43.2668,7.30811 42.6233,9.41766 46.6736,11.0375C 49.9291,12.3183 51.8975,14.0888 55.0393,11.7156C 57.197,10.058 59.8846,7.11975 62.383,9.64368C 64.3513,11.5649 67.5311,13.5991 65.487,16.5751C 63.8971,18.8353 60.9445,21.1709 62.1558,24.1469C 62.6858,25.5407 63.2914,26.8969 63.8592,28.253C 64.4271,29.7598 64.9192,30.6639 66.509,31.3044C 69.1588,32.3214 73.8905,31.0783 74.7611,34.5817C 74.799,34.7701 74.8368,35.0337 74.8747,35.3351L 74.8747,38.9892C 74.8368,39.4789 74.7611,39.8932 74.6854,40.1193C 74.1933,41.7391 72.9441,42.2289 71.3164,42.4549C 66.509,43.1329 65.2977,42.6432 63.7835,46.448C 62.1937,50.3657 60.7552,51.6842 63.1022,54.7732C 64.6542,56.8451 67.6825,59.595 65.1841,62.0813C 63.3671,63.8895 61.0581,67.3175 58.219,65.1703C 57.1213,64.3415 55.9857,63.5128 54.8879,62.684C 51.9732,60.4991 50.3833,61.9683 46.4465,63.5504C 44.9324,64.1532 44.0617,64.6053 43.4182,66.1874C 42.3205,68.8997 43.6454,73.3072 40.2385,74.3619C 40.0871,74.4373 39.7464,74.4749 39.33,74.5126L 35.5068,74.5126C 35.0904,74.4749 34.7876,74.4373 34.6362,74.3619C 31.1536,73.3072 32.5164,68.6737 31.3807,66.112C 30.6994,64.4922 29.8666,64.1155 28.3146,63.5128C 20.3275,60.3484 23.0529,61.0265 16.5421,65.208C 15.4065,65.9614 13.211,65.5847 12.1889,64.7182C 11.129,63.8518 10.1069,62.797 9.23631,61.7799C 8.6685,61.1018 8.36567,60.6875 8.51709,59.708C 9.04704,56.8074 14.3087,54.0197 12.681,50.2527C 11.394,47.2767 10.7126,43.9994 8.21426,43.1706C 5.48879,42.2665 0.870636,43.3966 0.0757075,39.7802C 0.0378537,39.6672 0.0378537,39.4789 0,39.2528L 0,35.2974C 0.0378537,34.8454 0.113561,34.4687 0.189269,34.2426C 0.90849,32.4721 1.89269,32.3214 3.67181,32.0201C 7.64645,31.342 9.42558,32.1707 11.129,27.9516C 12.4539,24.7496 14.1573,22.8284 11.7347,19.6264C 10.0691,17.4415 7.15436,14.9176 9.76626,12.3183C 11.5832,10.5101 13.7788,7.11975 16.7313,9.37997C 18.9269,11.0752 21.3495,13.7121 24.34,12.582C 25.7405,12.0923 27.1411,11.4519 28.5039,10.9245C 34.674,8.51355 29.6395,1.54446 34.7119,0.113007C 34.8633,0.0753174 35.204,0.0376587 35.6204,-3.05176e-005 Z " />
                <GeometryDrawing Brush="{StaticResource IconsBrush}" Geometry="M 74.1933,74.2866C 74.799,74.0229 76.4267,73.9475 77.1081,74.0229C 79.8335,74.3242 81.7262,76.0948 82.7861,78.5811C 83.4296,80.2009 83.0511,82.2728 82.3319,83.8173C 81.6126,85.3994 80.0228,86.4543 78.4708,87.0946C 76.8431,87.7727 74.7611,87.396 73.247,86.6426C 71.5057,85.8515 70.6351,84.4953 69.9158,82.8002C 69.2345,81.218 69.6509,79.1461 70.3701,77.6016C 71.1272,76.0194 72.6035,75.0023 74.1933,74.2866 Z M 102.205,72.4407L 102.886,74.8893C 102.962,75.3037 103,75.6427 103,75.7934C 102.886,77.1496 102.167,77.5263 101.069,78.016C 98.4954,79.1461 96.9056,79.1084 96.527,82.2351C 96.2242,84.7591 95.3536,86.4543 97.6248,88.1871C 99.1768,89.3925 101.751,90.711 100.464,92.9336C 99.4796,94.6664 98.6468,97.2657 96.1863,96.3616C 94.2936,95.7212 92.0981,94.0637 90.2054,95.4576C 89.3348,96.1356 88.502,96.776 87.6314,97.4541C 86.6851,98.1321 86.1551,98.6219 86.0037,99.865C 85.7766,101.899 87.5557,104.951 85.2087,106.231C 85.0952,106.307 84.9438,106.344 84.7167,106.457L 82.1426,107.135C 81.8019,107.173 81.4991,107.211 81.3098,107.211C 80.0606,107.173 79.4928,106.382 79.0386,105.327C 77.6759,102.088 77.7894,101.146 74.8368,100.807C 71.7707,100.43 70.5594,99.6767 68.8181,101.899C 67.6825,103.368 66.2819,106.005 64.0485,104.725C 62.4587,103.783 59.6196,102.803 60.6038,100.43C 60.9824,99.4883 61.3609,98.5842 61.7394,97.6424C 62.7236,95.1938 61.3609,94.4027 59.5439,91.9165C 58.8247,90.9747 58.3326,90.485 57.0834,90.3343C 55.0015,90.0706 52.1246,91.8035 50.724,89.6186C 50.6862,89.5432 50.5726,89.3172 50.4969,89.0535L 49.7777,86.3789C 49.702,86.0775 49.6641,85.8515 49.702,85.7385C 49.8155,83.1015 53.2981,83.1769 54.8879,81.9337C 55.91,81.105 56.0235,80.4646 56.1371,79.2591C 56.8942,73.1188 56.932,75.153 52.7303,71.4236C 52.011,70.7456 51.8596,69.1257 52.276,68.2593C 52.6924,67.3929 53.2224,66.4887 53.7902,65.66C 54.1687,65.1326 54.3958,64.8313 55.115,64.7936C 57.2348,64.6053 60.2253,67.7319 62.5344,65.886C 64.3892,64.4546 66.5847,63.3621 66.6983,61.4785C 66.8497,59.4066 65.1463,56.3553 67.5311,55.1499C 67.6446,55.1122 67.7582,55.0369 67.9096,54.9992L 70.6729,54.2458C 71.0136,54.2081 71.3164,54.1704 71.4678,54.2081C 72.8306,54.3588 73.1713,54.9992 73.7012,56.167C 74.9125,58.8416 74.6476,60.2354 77.9408,60.6498C 80.477,60.9511 82.1426,61.7799 83.9217,59.482C 85.1709,57.8998 86.3822,55.4136 88.6913,56.732C 90.319,57.6738 93.1202,58.5779 92.0603,61.0642C 91.3032,62.9101 89.9026,65.0949 91.2275,66.9785C 91.871,67.8449 92.5524,68.6737 93.1959,69.5401C 96.0349,73.3825 100.01,68.5606 101.978,71.838C 102.054,71.951 102.129,72.177 102.205,72.4407 Z " />
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

将画笔从资源字典传递到特殊类。

Messenger.Default.Send(Application.Current.FindResource("SettingIcon"));

如何为属性DrawingGeometry.Brush分配颜色。环境vs抛出异常?

public void LoadIcon(object obj)
{
  DrawingBrush brush = obj as DrawingBrush;
  DrawingGroup dgroup = brush.Drawing as DrawingGroup;    
  foreach (var item in dgroup.Children)
  {
    var geometry = item as GeometryDrawing;
    // This part throws an exception
    geometry.Brush = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));   
  };                
  IconButton = brush;
}

0 个答案:

没有答案