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

Hiding GridView when empty

7 Answers 728 Views
GridView
This is a migrated thread and some comments may be shown as answers.
haagel
Top achievements
Rank 1
haagel asked on 13 Apr 2010, 08:18 AM
I'm trying to hide my GridView when it is empty with a trigger, but I'm having trouble with that. The problem seems to be that Items.Count doesn't update when the ItemsSource of the GridView is updated.

I have a GridView and a TextBlock like this:

<telerikGridView:RadGridView Name="MyGrid" 
    ItemsSource="{Binding MyBindingList, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
    Style="{StaticResource MyGridStyle}"
    ... 
</telerikGridView:RadGridView> 
 
<TextBlock Text="No records..." Style="{StaticResource NoRecordsTextStyle}" /> 

And they reference these styles:

<Style x:Key="MyGridStyle" TargetType="telerikGridView:RadGridView"
    <Setter Property="Visibility" Value="Visible" /> 
    <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=MyGrid, Path=Items.Count}" Value="0"
            <Setter Property="Visibility" Value="Collapsed" /> 
        </DataTrigger> 
    </Style.Triggers> 
</Style> 
                    
<Style x:Key="NoRecordsTextStyle" TargetType="TextBlock"
    <Setter Property="Visibility" Value="Collapsed" /> 
    <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=MyGrid, Path=Items.Count}" Value="0"
            <Setter Property="Visibility" Value="Visible" /> 
        </DataTrigger> 
    </Style.Triggers> 
</Style> 

So the default is that the GridView is visible and the TextBlock is collapsed. When Items.Count is 0, i.e. when there are no records to show, the triggers make sure that the GridView is collapsed and the TextBlock is visible.

What happens is that the GridView never shows up but the TextBlock does, even though the GridView has records. If I use exactly the same code but swap out the GridView to a standard WPF ListBox it all works fine.

After some experimenting I've come to the conclusion that it is the Items.Count property that is the problem. It never gets updated. Initially the source of the GridView is empty and thus Items.Count is 0. But when I add items to the source (which I do immediately when the app starts) the Items.Count still remains 0.

Is this a known issue? How can I get around it? Or am I missing something?

Thanks // David

7 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 13 Apr 2010, 01:49 PM
Hi David Haglund,

I believe this blogpost deals with a problem simillar to yours.

Greetings,
Pavel Pavlov
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
haagel
Top achievements
Rank 1
answered on 13 Apr 2010, 02:27 PM
Thanks, but that is not really what I'm looking for. I want to hide the GridView completely when there are no records to display.

And as I said, I have code that work if I use a standard ListBox or DataGrid. The problem is that the Items.Count property on the GridView doesn't update as it should...
0
Maya
Telerik team
answered on 15 Apr 2010, 11:46 AM
Hi David Haglund,

We have tested the scenario, indeed there appears to be a problem with Items.Count used in a DataTrigger. Thank you for reporting the problem ! I have updated your Telerik points.

As a workaround I may suggest to use the Items.IsEmpty property instead (tested and working).

Something like:

<DataTrigger Binding="{Binding ElementName=myGrid, Path=Items.IsEmpty}" Value="True">
                   <Setter Property="Visibility" Value="Visible" />
               </DataTrigger>

Greetings,
Maya
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
haagel
Top achievements
Rank 1
answered on 19 Apr 2010, 02:53 PM
I'm afraid it doesn't work for me though... :(
0
Pavel Pavlov
Telerik team
answered on 20 Apr 2010, 12:11 PM
Hi David Haglund,

Please let me know the exact version of the telerik dlls you are using and  I wil prepare a working sample app for you .

Regards,
Pavel Pavlov
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
Brad
Top achievements
Rank 1
answered on 20 Sep 2011, 08:05 PM
This doesn't work for me either.  And I am on the latest build.  2011.2.712.40

<Style TargetType="{x:Type D:Hyperlink}" BasedOn="{StaticResource {x:Type D:Hyperlink}}">
    <Setter Property="IsEnabled" Value="True"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=dgTask, Path=Items.IsEmpty}" Value="True">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
0
Maya
Telerik team
answered on 23 Sep 2011, 12:01 PM
Hello Brad,

Could you share a bit more details for the exact scenario that you want to get ? Where is the Hyperlink defined - is it outside the grid or in a particular cell template ? Generally, any relevant information or code-snippet considering the grid would be helpful.
 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
haagel
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
haagel
Top achievements
Rank 1
Maya
Telerik team
Brad
Top achievements
Rank 1
Share this question
or