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

Setting Control Visibility based on Row Selection

3 Answers 134 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 2
Mike asked on 31 Aug 2011, 06:11 PM
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?

3 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 31 Aug 2011, 07:41 PM
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 >>

0
Mike
Top achievements
Rank 2
answered on 31 Aug 2011, 08:46 PM
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
0
Mike
Top achievements
Rank 2
answered on 31 Aug 2011, 09:28 PM
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
Tags
GridView
Asked by
Mike
Top achievements
Rank 2
Answers by
Vanya Pavlova
Telerik team
Mike
Top achievements
Rank 2
Share this question
or