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

Problem with custom column type

5 Answers 62 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tony
Top achievements
Rank 1
Tony asked on 25 Mar 2012, 02:50 AM
I have defined a custom grid view column type:
public class GridViewButtonColumn : GridViewBoundColumnBase {
    public static readonly DependencyProperty ButtonBackgroundProperty = . . .
    public static readonly DependencyProperty ButtonForegroundProperty = . . .
   
    public Brush ButtonBackground {
        get { return (Brush) GetValue( ButtonBackgroundProperty ); }
        set { SetValue( ButtonBackgroundProperty, value ); }
    }
      
    public Brush ButtonForeground {
        get { return (Brush) GetValue( ButtonForegroundProperty ); }
        set { SetValue( ButtonForegroundProperty, value ); }
    }
  
    public GridViewButtonColumn() {
        this.EditTriggers = GridViewEditTriggers.None;
    }
  
    public override FrameworkElement CreateCellElement( GridViewCell cell, object dataItem ) {
        Cell = cell;
        CellButton = new Button();
        CellButton.Click += new RoutedEventHandler( CellButton_Click );
        CellButton.CommandParameter = dataItem;
  
        Binding contentBinding = new Binding( "Content" ) {
            Source = this,
            Mode   = BindingMode.TwoWay
        };
        CellButton.SetBinding( Button.ContentProperty, contentBinding );
  
        Binding backgroundBinding = new Binding( "ButtonBackground" ) {
            Source = this,
            Mode = BindingMode.TwoWay
        };
        CellButton.SetBinding( Button.BackgroundProperty, backgroundBinding );
  
        Binding foregroundBinding = new Binding( "ButtonForeground" ) {
            Source = this,
            Mode = BindingMode.TwoWay
        };
        CellButton.SetBinding( Button.ForegroundProperty, foregroundBinding );
  
        if ( CellButtonVisibility != null ) {
            CellButton.SetBinding( Button.VisibilityProperty, CellButtonVisibility );
        }
  
        if ( this.DataMemberBinding != null ) {
            SetBinding( ContentProperty, this.DataMemberBinding );
        }
  
        GridViewRow row = cell.ParentRow as GridViewRow;
  
        row.SetBinding( GridViewRow.DetailsVisibilityProperty, new Binding( "Visibility" ) {
            Source = CellButton,
            Mode = BindingMode.TwoWay
        } );
  
        return CellButton;
    }
          
    void CellButton_Click( object sender, RoutedEventArgs e ) {
        Button btn = sender as Button;
        DataRetentionPolicy policy = btn.DataContext as DataRetentionPolicy;
              
        RoutedEventArgs newEventArgs = new ButtonColumnRoutedEventArgs( ClickEvent, policy );
        RaiseEvent( newEventArgs );
    }
}
Here's the xaml where I'm using it:

<telerik:RadGridView AutoExpandGroups="True"
                     AutoGenerateColumns="False"
                     Background="{DynamicResource DataBackground}"
                     CanUserDeleteRows="False"
                     CanUserFreezeColumns="False"
                     CanUserInsertRows="False"
                     CanUserResizeColumns="False"
                     CanUserSortColumns="True"
                     EnableColumnVirtualization="True"
                     EnableRowVirtualization="True"
                     FontSize="16"
                     FontWeight="Bold"
                     Foreground="{DynamicResource DataForeground}"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     Grid.Row="1"
                     IsReadOnly="True"
                     ItemsSource="{Binding Path=DataRetentionPolicies, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:AdvancedSettingEditor}}}"
                     Margin="5"
                     Name="DataPolicies"
                     SelectionUnit="FullRow"
                     ScrollMode="Deferred"
                     ScrollViewer.CanContentScroll="True"
                     ScrollViewer.HorizontalScrollBarVisibility="Auto"
                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                     ShowGroupFooters="True"
                     TabIndex="8"
                     ToolTip="Data Maintenance Properties"
                     Visibility="{Binding Converter={StaticResource BoolToVisibility}, Mode=TwoWay, Path=EnableRetention, RelativeSource={RelativeSource AncestorType={x:Type cs:AdvancedSettingEditor}}}">
    <telerik:RadGridView.Columns>
        <cs:GridViewButtonColumn ButtonBackground="{DynamicResource ButtonBackground}"
                                 ButtonForeground="{DynamicResource ButtonForeground}"
                                 Click="EditButton_Click"
                                 Content="Edit"
                                 FontSize="16"
                                 FontWeight="Bold"
                                 Width="75" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

The DynamicResources called ButtonBackground and ButtonForeground are SolidColorBrush resources defined in app.xaml.I have recently implemented themes in my application.  When the themes change, the colors of these resources, along with others, change. 

When the custom column is first displayed, the button colors are correct, but when the the theme changes, the colors aren't changing. How do I fix this problem?

Tony

5 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 29 Mar 2012, 07:05 AM
Hi,

I cannot be completely sure without a sample project, but I think that the problem is that these buttons does not reload their templates. You could try to reset the templates, something like:

var template = button.Template;
button.Template = null;
button.Template = template;

This will force calling of OnApplyTemplate method and everything should work.

Kind regards,
Nedyalko Nikolov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Tony
Top achievements
Rank 1
answered on 30 Mar 2012, 03:56 PM
Nedyalko:

Thank you, but I still have one question.

In order to do this, I'm going to have to iterate over all of the GridViewRow objects in the RadGridView control.  But how do I get a reference to the collection of GridViewRow objects?  The RadGridView's Items collection contains the object I put into the GridView.  There doesn't seem to be a public property or method for finding a particular GridViewRow object.

So where do I put the code you suggest using?

Tony
0
Nedyalko Nikolov
Telerik team
answered on 02 Apr 2012, 09:53 AM
Hello,

Indeed RadGridView has no rows collection property, since such collection is not usable (and reliable) due to UI virtualization. However you could use two different approaches to get the GridViewRow (container for an item).

this.radGridView.ItemContainerGenerator.ContainerFromItem(item) as GridViewRow;
 
or
 
this.radGridView.ChildrenOfType<GridViewRow>().Where(r => r.DataContext == item).First();

You will need to add using to Telerik.Windows.Controls to use ChildrenOfType<T> method.

Greetings,
Nedyalko Nikolov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Tony
Top achievements
Rank 1
answered on 03 Apr 2012, 08:33 PM
I tried using your technique, but it didn't work for me.  Here's the code I used:

private void OnTimeOfDayModeChanged( TimesOfDay newTimeofDayMode ) {
    foreach ( object item in DataPolicies.Items ) {
        GridViewRow row = DataPolicies.ItemContainerGenerator.ContainerFromItem( item ) as GridViewRow;
  
        if ( row != null ) {
            for ( int i = 0; i < 2; i++ ) {
                GridViewButtonColumn column = row.Cells[ i ].Column as GridViewButtonColumn;
                if ( column != null ) {
                    if ( column.CellButton != null ) {
                        var template = column.CellButton.Template;
                        column.CellButton.Template = null;
                        column.CellButton.Template = template;
                    }
                }
            }
        }
    }
}

I've already posted the code for the GridViewButtonColumn class in an earlier posting in this thread.

I don't understand what I'm doing wrong.  The code had no effect on the appearance of the buttons.  How do I get the effect I want?

Tony
0
Nedyalko Nikolov
Telerik team
answered on 04 Apr 2012, 12:55 PM
Hi,

Unfortunately without a sample project I cannot give you any suggestions.
Could you send me a small repro project which I can debug on my side in order to see what is going on?

All the best,
Nedyalko Nikolov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
GridView
Asked by
Tony
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Tony
Top achievements
Rank 1
Share this question
or