Setting Control Visibility based on Row Selection

4 posts, 0 answers
  1. Mike
    Mike avatar
    32 posts
    Member since:
    May 2011

    Posted 31 Aug 2011 Link to this post

    Hi,

    I need to make a RadButton visible when the row that contains it is selected and collapsed when its row is not selected. The button is placed in the last column. Ideally, I'd like to bind the Visibility property to a property on the row and use a converter to return Visible or Collapsed.

    Here is the XAML:

    <telerik:RadGridView Grid.Row="1" Grid.Column="0" x:Name="myNotificationsGv" ItemsSource="{Binding Path=MyNotificationMessages}" Controls:StyleManager.Theme="Vista"
            AutoGenerateColumns="False" IsReadOnly="True" RowIndicatorVisibility="Collapsed" ag1:RadGridViewHeaderMenu.IsEnabled="True" ShowGroupPanel="False"
            DataLoadMode="Synchronous" IsSynchronizedWithCurrentItem="true" ScrollMode="Deferred" telerikGridViewFilter:CustomFilterRow.IsEnabled="True" SelectionUnit="FullRow"
            VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionChanged="myNotificationsGvSelectionChanged">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn Header="{Binding Path=NotificationStrings.NameColumnTitle, Source={StaticResource ResourceWrapper}}"
                    DataMemberBinding="{Binding Path=JobName}" />
            <telerik:GridViewDataColumn Header="{Binding Path=NotificationStrings.OrganizationColumnTitle, Source={StaticResource ResourceWrapper}}"
                    DataMemberBinding="{Binding Path=OrganizationName}" />
            <telerik:GridViewDataColumn Header="{Binding Path=NotificationStrings.TypeColumnTitle, Source={StaticResource ResourceWrapper}}"
                    DataMemberBinding="{Binding Path=NotificationMessageType.Name}" />
            <telerik:GridViewDataColumn Header="{Binding Path=NotificationStrings.DateColumnTitle, Source={StaticResource ResourceWrapper}}"
                    DataMemberBinding="{Binding Path=DateCreated, StringFormat='dd MMM yyyy'}" MaxWidth="200" />
            <telerik:GridViewColumn Width="90">
                <telerik:GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Controls:RadButton x:Name="ForwardBtn" Content="{Binding Path=NotificationStrings.Forward, Source={StaticResource ResourceWrapper}}"
                                Tag="{Binding Path=Id}" Click="ForwardBtn_Click" Style="{StaticResource ForwardButtonStyle}" Visibility="Collapsed" />
                    </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
            </telerik:GridViewColumn>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>


    I've tried using the SelectionChanged event handler, but just can't seem to find the button that lives in the selected row. 

    Any suggestions?
  2. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 31 Aug 2011 Link to this post

    Hello Mike,

     

    Within the Button's Click event handler you may get its parent row using the ParentOfType extension method. Then based on the DataContext of this row you may access any properties of your object and based on a property value you may set the Visibility property of this button. Furthermore you may check whether the IsSelected property of the row is set to true and hide the button through setting its Visibility property to Collapsed.  



    Regards,
    Vanya Pavlova
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft banner
  4. Mike
    Mike avatar
    32 posts
    Member since:
    May 2011

    Posted 31 Aug 2011 Link to this post

    Vanya,

    Thanks for the quick reply. However, I'm not using the button yet at the time that I need to set its visibility so the click event is inappropriate. Right now I'm trying to set the button's visibility in the SelectionChanged event handler. I am using the following to find the button.

    var row = myNotificationsGv.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]) as GridViewRow;
    if (row == null) return;
     
    var button = row.FindChildByType<RadButton>();

    This works fine if the user clicks on a row. However, the first time through when the grid is loaded and the first row is selected by default, the row variable is null. I'm probably going to have to use the RowLoaded event to set the visibility of the button in the first row loaded. This doesn't address setting the button visibility in the row being deselected.

    Thanks,

    Mike
  5. Mike
    Mike avatar
    32 posts
    Member since:
    May 2011

    Posted 31 Aug 2011 Link to this post

    Vanya,

    Just an FYI and a tie up for this thread. I used the following code in the SelectionChanged event handler to show and hide the button as I wished. (I've checked the e.AddedItems.Count > 0 a ways above, this isn't shown in the snippet.)

    var row = myNotificationsGv.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]) as GridViewRow;
    if (row == null) return;
     
    var button = row.FindChildByType<RadButton>();
     
    button.Visibility = Visibility.Visible;
     
    if (e.RemovedItems.Count == 0) return;
     
    row = myNotificationsGv.ItemContainerGenerator.ContainerFromItem(e.RemovedItems[0]) as GridViewRow;
    if (row == null) return;
     
    button = row.FindChildByType<RadButton>();
     
    button.Visibility = Visibility.Collapsed;


    Thanks,

    Mike
Back to Top