This is a migrated thread and some comments may be shown as answers.

Getting Property values for GridViewHeaderCell from a Theme

6 Answers 163 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 01 Jul 2010, 10:04 PM
I'm having trouble trying to obtain/apply a default property value from a theme.  Certain actions in my application change the GridViewHeaderCell's foreground property to a color different than that of the theme.  After it changes, I cannot figure out an easy way to change the color back to the theme's normal color.

Here is how I set the style to change the Header cells' color.
foreach(Telerik.Windows.Controls.GridViewColumn column in GridView.Columns) 
   Style s = new Style(typeof(GridViewHeaderCell), column.HeaderCellStyle); 
   s.Setters.Add(new Setter(GridViewHeaderCell.ForegroundProperty, new SolidColorBrush(Colors.Red))); 
   s.Seal(); 
   column.HeaderCellStyle = s; 

To Change it back to the default, I've tried the following
column.ClearValue(GridViewHeaderCell.ForegroundProperty); //no noticeable impact 

column.ClearValue(GridViewColumn.HeadCellStyleProperty);  //no noticeable impact 

 
//this makes the foreground (some text) disappear altogether 
Style s = new Style(typeof(GridViewHeaderCell), column.HeaderCellStyle); 
s.Setters.Add(new Setter(GridViewHeaderCell.ForegroundProperty, null)); 
s.Seal(); 
column.HeaderCellStyle = s; 

None of those three ways seem to do the trick.  I tried clearing the value with the hope that the Theme's default header cell foreground property would take place once the local value is removed, but it doesn't seem to do anything.  

I have also tried getting and storing the default value before I change it (via GridViewHeaderCell.ForegroundProperty.GetMetadata(typeof(GridViewHeaderCell))...) but the PropertyMetaData object returned always indicates that the default Foreground for GridViewHeaderCell is a black brush, even when using themes like Office Black or Vista, which clearly do not use a black brush in their foregrounds. 

Has anyone dealt with this before, or know where I'm going wrong? If anyone can help me figure out the right way to do it, I will be most grateful.

Thanks,
Tom

6 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 05 Jul 2010, 10:07 AM
Hi Tom,

 DependencyObject.ClearValue is the right way to go. However, you shouldn't call ClearValue on the GridViewColumn when you actually need to performs the operation on the GridViewHeaderCell.

Please try the following:

column.FindChildByType<GridViewHeaderCell>().ClearValue(GridViewHeaderCell.ForegroundProperty);

Regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Tom
Top achievements
Rank 1
answered on 05 Jul 2010, 02:57 PM
Hi Yavor

That doesn't seem to work.  The FindChildByType call is failing with an InvalidOperationException: Telerik.Windows.Controls.GridViewDataColumn' is not a Visual or Visual3D.

I made a simple grid with a button to change the header cell color to test it.  Here is the color changing code:
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            if (showColor = !showColor) //toggle the color with every click of the button 
            { 
                SolidColorBrush brush = new SolidColorBrush(Colors.Red); 
                foreach (Telerik.Windows.Controls.GridViewColumn column in GridView.Columns) 
                { 
                    Style s = new Style(typeof(GridViewHeaderCell), column.HeaderCellStyle); 
                    s.Setters.Add(new Setter(GridViewHeaderCell.ForegroundProperty, brush)); 
                    s.Seal(); 
                    column.HeaderCellStyle = s; 
                } 
            } 
            else 
            { 
                foreach (Telerik.Windows.Controls.GridViewColumn column in GridView.Columns) 
                    column.FindChildByType<GridViewHeaderCell>().ClearValue(GridViewHeaderCell.ForegroundProperty); 
            } 
        } 


And here is the wonderful stack trace:
System.InvalidOperationException was unhandled 
  Message='Telerik.Windows.Controls.GridViewDataColumn' is not a Visual or Visual3D. 
  Source=PresentationCore 
  StackTrace: 
       at MS.Internal.Media.VisualTreeUtils.AsVisual(DependencyObject element, Visual& visual, Visual3D& visual3D) 
       at MS.Internal.Media.VisualTreeUtils.AsNonNullVisual(DependencyObject element, Visual& visual, Visual3D& visual3D) 
       at System.Windows.Media.VisualTreeHelper.GetChildrenCount(DependencyObject reference) 
       at Telerik.Windows.Controls.DependencyObjectExtensions.<FindChildrenByType>d__0`1.MoveNext() in c:\Builds\WPF_Scrum\Core_WPF\Sources\Development\Core\Controls\Extensions\DependencyObjectExtensions.cs:line 50 
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) 
       at Telerik.Windows.Controls.DependencyObjectExtensions.FindChildByType[T](DependencyObject source) in c:\Builds\WPF_Scrum\Core_WPF\Sources\Development\Core\Controls\Extensions\DependencyObjectExtensions.cs:line 40 
       at CustomGroupingGrid.MainWindow.Button_Click(Object sender, RoutedEventArgs e) in C:\Users\thomasw\documents\visual studio 2010\Projects\CustomGroupingGrid\CustomGroupingGrid\MainWindow.xaml.cs:line 62 
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 
       at System.Windows.Controls.Primitives.ButtonBase.OnClick() 
       at System.Windows.Controls.Button.OnClick() 
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 
       at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) 
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
       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.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) 
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
       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.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) 
       at System.Windows.Input.InputManager.ProcessStagingArea() 
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) 
       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.InvokeImpl(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.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
       at System.Windows.Threading.Dispatcher.Run() 
       at System.Windows.Application.RunDispatcher(Object ignore) 
       at System.Windows.Application.RunInternal(Window window) 
       at System.Windows.Application.Run(Window window) 
       at System.Windows.Application.Run() 
       at CustomGroupingGrid.App.Main() in C:\Users\thomasw\documents\visual studio 2010\Projects\CustomGroupingGrid\CustomGroupingGrid\obj\x86\Debug\App.g.cs:line 0 
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
       at System.Threading.ThreadHelper.ThreadStart() 
  InnerException:  
 


EDIT: This is happening in both version 2010.2.609.35 and 2010.1.625.35
-Tom
0
Accepted
Yavor Georgiev
Telerik team
answered on 05 Jul 2010, 03:25 PM
Hi Tom,

 Could you please try this then:

var headerCell = this.radGridView1.ChildrenOfType<GridViewHeaderCell>().Where((c) => c.Column == column).FirstOrDefault();
headerCell.ClearValue(GridViewHeaderCell.ForegroundProperty);

Please let me know how it goes.

All the best,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Tom
Top achievements
Rank 1
answered on 05 Jul 2010, 04:04 PM
That gets me the proper references to the GridViewHeaderCell objects, but calling ClearValue on the cell is not reverting the style back to the theme's default (the foreground remains red after ClearValue is called).  Is there something wrong with the way I am setting the color in the first place?

Thanks,
Tom
0
Yavor Georgiev
Telerik team
answered on 05 Jul 2010, 04:08 PM
Hi Tom,

 In the code where you set the Foreground brush, try getting the GridViewHeaderCell directly and setting its Foreground property, instead of applying a style, because you'll have to clear the HeaderCellStyle property of the column, too.

Kind regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Tom
Top achievements
Rank 1
answered on 05 Jul 2010, 04:14 PM
That did it.  Thanks!
Tags
GridView
Asked by
Tom
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Tom
Top achievements
Rank 1
Share this question
or