Reason xamDataGrid for redrawing

I need the viewmodel to instruct the XamDataGrid in the view to just re-read and recolor its cells with minimal problems. I don’t want to mess with the source and do some unstable workarounds with increasing its events (the source may change).

To make it more understandable, I have a global static class that contains some configuration of visual signals that do not affect the data, but only they are presented in the grid (scaling, formatting, etc.). The visual action takes place in the implementation of IValueConverter , attached to a field that works fine. There is a static event that fires when the cues change, and viewmodels subscribe to it, and events fire properly. Now I just need the event handler to call the grid redraw.

Any suggestions?

EDIT: some code if this helps:

Converter

 public class ScaleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (targetType == typeof(double) || targetType == typeof(double?)) { if (value == null && targetType == typeof(double?)) return null; // short circuit for null->null, nothing to scale if (value is double) return (double)value / DisplayScale.Scale; // shortcut for direct scaling, no need to waste time on conversion try { return System.Convert.ToDouble(value) / DisplayScale.Scale; // for convertible values, eat conversion exception } catch (Exception) {}; // if all esle fails return NaN return double.NaN; } // fallthrough, return null, this should not happen, if it does it means converter is incomplete return null; } ... } 

DisplayScale global

 public class DisplayScale: NotificationObject { private static KeyValuePair<string, double> _ActiveScaling; private static readonly object _lockHandle = new object(); // note: should not contest, but just to be on the safe side public static event Action ScalingChanged; public static List<KeyValuePair<string, double>> ScaleList { get; private set; } static DisplayScale() { ScaleList = new List<KeyValuePair<string, double>>() { new KeyValuePair<string, double>("No scaling (1's)", 1d), new KeyValuePair<string, double>("Thousands (1,000's)", 1000d), new KeyValuePair<string, double>("Millions (1,000,000's)", 1000000d), new KeyValuePair<string, double>("Billions (1,000,000,000's)", 1000000000d) }; ActiveScaling = ScaleList.First(); // OPTION: could be in per-user config } public static double Scale { get { return ActiveScaling.Value; } } public static KeyValuePair<string, double> ActiveScaling { get { lock (_lockHandle) return _ActiveScaling; } set { lock (_lockHandle) { _ActiveScaling = value; var eventCall = ScalingChanged; if (eventCall != null) eventCall(); } } } } 
Fields

defined as

 // resource <inf:ScaleConverter x:Key="ScaleConverter" /> // field <igDP:Field Name="Deposit" Converter="{StaticResource ScaleConverter}"> 
+4
source share
2 answers

if you have a collection view than a call, just Refresh ().

 public class YourViewModel { private ObservableCollection<YourDataClass> yourColl; public void YourViewModel() { yourColl = new ObservableCollection<YourDataClass>(); YourCollectionView = CollectionViewSource.GetDefaultView(yourColl); DisplayScale.ScalingChanged += () => YourCollectionView.Refresh(); } var ICollectionView yourCollView; public ICollectionView YourCollectionView { get { yourCollView; } set { yourCollView = value; RaisePropertyChanged("YourCollectionView"); } } } 
+4
source

I had the same problem , and it turns out that even if I had my ViewModel implementation with INotifyPropertyChanged, I did not implement INotifyPropertyChanged in the class that the grid lines were bound to my ObservableCollection (i.e. YourDataClass answer in punker76). After I implemented it in this class, it all started with an update, as expected.

I know that you mentioned “I do not want to contact the source”, so I’m not sure that this solution is acceptable for you, but I thought that I would share with others who also find this post.

0
source

Source: https://habr.com/ru/post/1384557/


All Articles