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

[Solved] Show cell contents on row select

3 Answers 166 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Aaron
Top achievements
Rank 1
Aaron asked on 09 Feb 2011, 11:21 PM
I have a button column and by default each button for each row is hidden (the column is always showing). I need to show the button when the row is selected. How do I do that?

<telerik:GridViewColumn UniqueName="ButtonColumn" MinWidth="10">
    <telerik:GridViewColumn.CellTemplate>
        <DataTemplate>
            <telerik:RadButton Background="Black" Content="View"/>
        </DataTemplate>
    </telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>

Thanks,
Aaron

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 10 Feb 2011, 08:40 AM
Hello Aaron,

There are a couple of options depending on your particular scenario. If you want to make a button visible once its corresponding item is selected, you may handle the SelectionChanged event as follows:

private void clubsGrid1_SelectionChanged(object sender, SelectionChangeEventArgs e)
    {
        var row = this.clubsGrid1.ItemContainerGenerator.ContainerFromItem(this.clubsGrid1.SelectedItem) as GridViewRow;
        var button = row.ChildrenOfType<RadButton>().FirstOrDefault();
        if(button !=null)
        {
            button.Visibility = System.Windows.Visibility.Visible;
        }          
    }

In this case however, the buttons will not be collapsed once a new item is selected. If you require only a single item to have a visible button, you may handle the SelectionChanged event as follows:
private Club lastSelectedItem;
        private void clubsGrid1_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (lastSelectedItem != null)
            {
                lastSelectedItem.IsSelected = false;
            }
 
            this.lastSelectedItem = this.clubsGrid1.SelectedItem as Club;
            this.lastSelectedItem.IsSelected = true;           
        }

I am sending you a sample project illustrating the latest approach.
 


Regards,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Aaron
Top achievements
Rank 1
answered on 10 Feb 2011, 06:17 PM
Hi Maya,
That worked great! I altered your solution in that I make the button object global so when the selection occurs I can set the global button as collapsed and then reassign the global button as the newly selected row's button. That works very well. There is one issue I'm seeing which I can't seem to figure out. In my grid in the first column I have a thumbnail that with a button click, we'll call it 'thumbnail button' will enlarge the thumbnail. Also when you click the 'thumbnail button' two columns are hidden and another one is shown. When I click the thumbnail button, the button inside the grid in the button column is hidden and nothing I do can get that the button to show unless I select another row and reselect the previous row. I have a sample project if you would like otherwise here is the trimmed down version of the xaml and code. If you look at the vb code below in the Button1_Click code, I try and get the SelectedRow again but it returns nothing, even though the row appears to be still selected.
Let me know if you want the entire project.
Thanks again,
Aaron

<telerik:RadGridView Grid.Row="1" d:LayoutOverrides="Width, Height" Name="FileList" CanUserReorderColumns="False" IsFilteringAllowed="False" IsReadOnly="True" CanUserFreezeColumns="False" CanUserResizeColumns="False" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" CanUserInsertRows="False" CanUserDeleteRows="False" BorderThickness="0" AutoGenerateColumns="False" RowHeight="20" ScrollViewer.VerticalScrollBarVisibility="Auto" AlternateRowBackground="#FFF0F0F0" AlternationCount="2" >
    <telerik:RadGridView.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="White" Offset="0"/>
            <GradientStop Color="#FFCFD3D8" Offset="1"/>
            <GradientStop Color="#FFE9E9EA" Offset="0.659"/>
            <GradientStop Color="#FFF4F4F5" Offset="0.375"/>
        </LinearGradientBrush>
    </telerik:RadGridView.Background>
    <telerik:RadGridView.Columns>
        <telerik:GridViewImageColumn UniqueName="ThumbnailColumn" DataMemberBinding="{Binding ThumbnailURL}" Header="" Width="40" Background="Transparent"/>
        <telerik:GridViewDataColumn UniqueName="FileDescriptionColumn" DataMemberBinding="{Binding Description}" Header="File" Width="*" IsVisible="True"/>
        <telerik:GridViewDataColumn UniqueName="FolderDescriptionColumn" DataMemberBinding="{Binding FolderDisplay}" Header="Folder" Width="80" IsVisible="True"/>
        <telerik:GridViewDataColumn UniqueName="CabinetColumn" DataMemberBinding="{Binding CabinetName}" Header="Cabinet" Width="80" IsVisible="True" />
        <telerik:GridViewDataColumn UniqueName="AllDataColumn" IsVisible="False" Width="*" >
            <telerik:GridViewDataColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel >
                        <TextBox Text="File" Foreground="White" IsReadOnly="True"  >
                            <TextBox.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF5B5B5B" Offset="1"/>
                                    <GradientStop Color="#FF868686"/>
                                    <GradientStop Color="#FF4F4F4F" Offset="0.42"/>
                                    <GradientStop Color="#FF0E0E0E" Offset="0.43"/>
                                </LinearGradientBrush>
                            </TextBox.Background>
                        </TextBox>
                        <TextBox Text="{Binding Description}" Background="Transparent" IsReadOnly="True" >
                        <TextBox Text="Folder" Foreground="White" IsReadOnly="True" >
                            <TextBox.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF5B5B5B" Offset="1"/>
                                    <GradientStop Color="#FF868686"/>
                                    <GradientStop Color="#FF4F4F4F" Offset="0.42"/>
                                    <GradientStop Color="#FF0E0E0E" Offset="0.43"/>
                                </LinearGradientBrush>
                            </TextBox.Background>
                        </TextBox>
                        <TextBox Text="{Binding FolderFirstDisplay}" Background="Transparent" IsReadOnly="True" >
                        <TextBox Text="Cabinet" Foreground="White" IsReadOnly="True" >
                            <TextBox.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF5B5B5B" Offset="1"/>
                                    <GradientStop Color="#FF868686"/>
                                    <GradientStop Color="#FF4F4F4F" Offset="0.42"/>
                                    <GradientStop Color="#FF0E0E0E" Offset="0.43"/>
                                </LinearGradientBrush>
                            </TextBox.Background>
                        </TextBox>
                        <TextBox Name="CabinetNameTextbox" Text="{Binding CabinetName}" Background="Transparent" IsReadOnly="True" >
                    </StackPanel>
                </DataTemplate>
            </telerik:GridViewDataColumn.CellTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewColumn UniqueName="PropertiesColumn" IsVisible="True" MinWidth="10" >
            <telerik:GridViewColumn.CellTemplate>
                <DataTemplate>
                    <telerik:RadButton Visibility="Collapsed"  Background="Black" Content="."/>
                </DataTemplate>
            </telerik:GridViewColumn.CellTemplate>
        </telerik:GridViewColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

Private CurrentThumbnailView As ThumbnailViewModes
Private _FileInfoButton As RadButton = Nothing
Public Enum ThumbnailViewModes
    SmallThumbnails
    MediumThumbnails
    LargeThumbnails
End Enum
Private Sub FileList_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles FileList.SelectionChanged
    If FileList.SelectedItem IsNot Nothing Then
        If _FileInfoButton IsNot Nothing Then
            _FileInfoButton.Visibility = Windows.Visibility.Collapsed
        End If
        Dim SelectedRow = TryCast(FileList.ItemContainerGenerator.ContainerFromItem(FileList.SelectedItem), Telerik.Windows.Controls.GridView.GridViewRow)
        If SelectedRow IsNot Nothing Then
            _FileInfoButton = SelectedRow.ChildrenOfType(Of RadButton)().FirstOrDefault()
            _FileInfoButton.Visibility = Windows.Visibility.Visible
        End If
    Else
    End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles changethumbnail.Click
    Dim mode As ThumbnailViewModes
    If CurrentThumbnailView = 2 Then
        mode = 0
    Else
        mode = CurrentThumbnailView + 1
    End If
    Select Case mode
        Case ThumbnailViewModes.SmallThumbnails
            FileList.RowHeight = 20
            FileList.Columns("ThumbnailColumn").Width = 40
            FileList.Columns("FileDescriptionColumn").IsVisible = True
            FileList.Columns("FolderDescriptionColumn").IsVisible = True
            FileList.Columns("CabinetColumn").IsVisible = True
            FileList.Columns("AllDataColumn").IsVisible = False
        Case ThumbnailViewModes.MediumThumbnails
            FileList.RowHeight = 100
            FileList.Columns("ThumbnailColumn").Width = 100
            FileList.Columns("AllDataColumn").IsVisible = True
            FileList.Columns("FileDescriptionColumn").IsVisible = False
            FileList.Columns("FolderDescriptionColumn").IsVisible = False
            FileList.Columns("CabinetColumn").IsVisible = False
        Case ThumbnailViewModes.LargeThumbnails
            FileList.RowHeight = 160
            FileList.Columns("ThumbnailColumn").Width = 160
            FileList.Columns("FileDescriptionColumn").IsVisible = False
            FileList.Columns("FolderDescriptionColumn").IsVisible = False
            FileList.Columns("CabinetColumn").IsVisible = False
            FileList.Columns("AllDataColumn").IsVisible = True
    End Select
    CurrentThumbnailView = mode
    Dim SelectedRow = TryCast(FileList.ItemContainerGenerator.ContainerFromItem(FileList.SelectedItem), Telerik.Windows.Controls.GridView.GridViewRow)
    If SelectedRow IsNot Nothing Then
        _FileInfoButton = SelectedRow.ChildrenOfType(Of RadButton)().FirstOrDefault()
        _FileInfoButton.Visibility = Windows.Visibility.Visible
    End If
End Sub
0
Maya
Telerik team
answered on 16 Feb 2011, 09:31 AM
Hello Aaron,

I was not able to reproduce the issue you reported. I am sending you the updated sample project using the code-snippets you provided. Please take a look at it and let me know in case of any misunderstandings. Feel free to change the application so that it meets your requirements and send it back if necessary. In case it is more appropriate send us your custom project. 

All the best,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
Aaron
Top achievements
Rank 1
Answers by
Maya
Telerik team
Aaron
Top achievements
Rank 1
Share this question
or