Zero results message in the WPF Grid
I need to display search results in the Grid format. But when the S开发者_C百科earch results in zero matches, i should be able to show a message "0 results found" in the second row of the Grid with first row having all the column headers.
Which control can i use in the WPF to achieve this? There are ways to do this without header row, but i need to display the Column Header with the message in the data rows.
Use the same approach as the question you mention, but keep the list visible.
<ContentPresenter Content="{Binding}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Grid>
<ListView Name="list" ItemsSource="{Binding MyList}"/>
<TextBlock Name="empty" Text="No items found" Visibility="Collapsed"/>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding MyList.Count}" Value="0">
<Setter TargetName="empty" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
Too simple? Am I missing something?
I'd probably go for the approach of setting up your Grid with your headers and your results row(s) - if you're using the Grid layout control then I assume you're creating row definitions to hold the results, or just using a StackPanel in your second row to hold the results, if you're using a special DataGrid-esque control then that's fine.
If you're using a DataGrid type control then I'd check the documentation for that, what you're looking for may already be provided.
Failing that, I'd create a StackPanel to hold your "No search results" message and an ItemsControl that holds your actual search results and use Style data triggers to toggle the visibility based on whether there are any results or not.
The XAML is likely to be wrong because I'm free-writing, but it should illustrate the basic idea:
<Grid x:Name="searchResultsGrid">
<Grid.RowDefinitions>
// Header row definition here
// Results row definition here
</Grid.RowDefinitions>
<Grid x:Name="headers" Grid.Row="0">Headers would go here</Grid>
<Grid x:Name="results" Grid.Row="1">
<StackPanel x:Name="noResults">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding SearchResultsFound}" Value="False">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding SearchResultsFound}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
<ItemsControl x:Name="resultsControl" ItemsSource="{Binding SearchResults}">
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding SearchResultsFound}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding SearchResultsFound}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
</ItemsControl>
</Grid>
</Grid>
精彩评论