Hello, I have Silverlight 5 application in which we used Infragistic controls. Now we are converting few views to WPF using MVVM. In xamDataGrid, we make few rows as BOLD based on condition as well as we set background color of few CELLS based on other condition. So in Silverlight, we capture "InitializeRow" and "CellControlAttached" events and achieve it using following code.
private void dataCFGrid_InitializeRow(object sender, InitializeRowEventArgs e) {int index = e.Row.Index;if (index == 10 || index == 18 || index == 25 || index == 28) { Style style0 = new Style(typeof(CellControl)); style0.Setters.Add(new Setter(CellControl.BackgroundProperty, "#dddddd")); style0.Setters.Add(new Setter(CellControl.FontWeightProperty, FontWeights.Bold)); style0.Setters.Add(new Setter(CellControl.BorderThicknessProperty, "0,0,0,0")); style0.Setters.Add(new Setter(CellControl.VerticalAlignmentProperty, VerticalAlignment.Top));
Style style = new Style(typeof(CellControl)); style.Setters.Add(new Setter(CellControl.FontWeightProperty, FontWeights.Bold)); style.Setters.Add(new Setter(CellControl.BorderThicknessProperty, "0")); style.Setters.Add(new Setter(CellControl.VerticalAlignmentProperty, VerticalAlignment.Top)); for (int i = 0; i < e.Row.Cells.Count; i++) { if (i == 0) e.Row.Cells[i].Style = style0; else e.Row.Cells[i].Style = style; } } else if (index == 0 || index == 11 || index == 19 || index == 26 || index == 29) e.Row.Height = new RowHeight(12); }
and
private void dataCFGrid_CellControlAttached(object sender, CellControlAttachedEventArgs e) { int rIndex = e.Cell.Row.Index; if (e.Cell.Column.HeaderText.Contains("E") || e.Cell.Column.HeaderText.Contains("T")) { if (Constants.CFEditableRows.Contains(rIndex)) { Style s = new Style(typeof(CellControl)); s.Setters.Add(new Setter(CellControl.BackgroundProperty, "#99CCFF")); e.Cell.Style = s; } } }
1. So my first question is that how can I capture these events in my ViewModel?
2. (Important) I tried to use same logic (i.e. code behind event handlers) in WPF MVVM project but get exception when I set "BorderThickness" in InitializeRow event OR Background color of Cells in InitializeRow or CellControlAttached Event.
For example this exception occur on following line of InitializeRow event handler "style0.Setters.Add(new Setter(CellControl.BackgroundProperty, System.Drawing.Color.Gray));"
System.ArgumentException was unhandled HResult=-2147024809 Message='Color [Gray]' is not a valid value for the 'System.Windows.Controls.Panel.Background' property on a Setter. Source=PresentationFramework StackTrace: at System.Windows.Setter.Seal() at System.Windows.SetterBaseCollection.Seal() at System.Windows.Style.Seal() at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache) at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at Infragistics.Controls.Grids.CellBase.ApplyStyle() at Infragistics.Controls.Grids.CellBase.OnElementAttached(CellControlBase element) at Infragistics.RecyclingContainer`1.Infragistics.ISupportRecycling.OnElementAttached(FrameworkElement elem) at Infragistics.RecyclingManager.PanelInfo.AttachElement(ISupportRecycling obj) at Infragistics.RecyclingManager.AttachElement(ISupportRecycling obj, Panel parent) at Infragistics.Controls.Grids.Primitives.CellsPanel.RenderCell(Column column, Collection`1 starColumns, Double& maxHeight, Boolean insert, Collection`1 visibleCells, Boolean isInfinite, Boolean suppressCellControlAttached) at Infragistics.Controls.Grids.Primitives.CellsPanel.RenderCell(Column column, Collection`1 starColumns, Double& maxHeight, Boolean insert, Collection`1 visibleCells, Boolean isInfinite) at Infragistics.Controls.Grids.Primitives.CellsPanel.RenderCells(Double availableWidth) at Infragistics.Controls.Grids.Primitives.CellsPanel.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Infragistics.Controls.Grids.Primitives.RowsPanel.RenderRow(RowBase row, Double availableWidth) at Infragistics.Controls.Grids.Primitives.RowsPanel.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Infragistics.Controls.Grids.XamGrid.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.Controls.TabItem.OnPreviewGotKeyboardFocus(KeyboardFocusChangedEventArgs e) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed) at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed) at System.Windows.Input.KeyboardDevice.Focus(IInputElement element) at System.Windows.UIElement.Focus() at System.Windows.Controls.TabItem.SetFocus() at System.Windows.Controls.TabItem.OnMouseLeftButtonDown(MouseButtonEventArgs e) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run() at SavaNet.Desktop.Studio.Dev.App.App.Main() in e:\SavaNet\Code\SavaNet.Desktop.Studio\Dev\SavaNet.Desktop.Studio.Dev.App\obj\Debug\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
Any help to resolve this error in WPF will be highly appreciated.
Hello,
I am just checking if you got this worked out or you still require any assistance or clarification on the matter.
Thank you Stefan for you response.
But the problem in my code was"String" (i.e. I was assigning string to color and border)......When I use proper Objects like
style0.Setters.Add(new Setter(CellControl.BackgroundProperty, new SolidColorBrush(Colors.Gray)));style0.Setters.Add(new Setter(CellControl.FontWeightProperty, FontWeights.Bold));style0.Setters.Add(new Setter(CellControl.BorderThicknessProperty, new Thickness(0)));
then it solve the problem and everything is working perfectly.