Display Message in WPF listBox When ItemSource is Null

Kailash Chandra Behera | Friday, August 11, 2023

WPF listBox

Display Message in WPF listBox When ItemSource is Null


 

This blog shares some XAML code that "No Data to Display" message displays in WPF ListBox when the list of data is empty. Generally, we are listing data in a ListBox for report purposes and we display the ItemControls blank when the function you have used to fetch data from the data source returns a list of data or null.

if you can display a cute message rather than displaying blank list data in WPF ItemControls then an awesome UI (WPF windows)will be created in WPF. The following XAML code examples customize the WPF style for WPF listBox to display "No Data to Display" message when the list of data(Item source) is empty or null.

 <ListBox>  
      <ListBox.Style>  
           <Style TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">  
                <Style.Triggers>  
                     <DataTrigger   
      Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}"   
      Value="0">  
                          <Setter Property="Template">  
                               <Setter.Value>  
                                    <ControlTemplate>  
                                              <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontFamily="Bell MT" FontSize="48">No Data to Display</TextBlock>  
                                    </ControlTemplate>  
                               </Setter.Value>  
                          </Setter>  
                     </DataTrigger>  
                </Style.Triggers>  
           </Style>  
      </ListBox.Style>  
 </ListBox>  

Elements Used in WPF ListBox

WPF Binding

Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source, and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well.

WPF Style

In WPF style is used to design a WPF control like background color, border color etc. it enables the sharing of properties, resources, and event handlers between instances of a type. Normally a style is an unique object which is used to style WPF controls. Each WPF element contains a number of Dependency Properties.

WPF Control Template

WPF Control Template is one of the wpf templates which defines the visual appearance and visual behavior of WPF control, for example, a button controller has a shape and it changes background color when the mouse clicks and hovers, Every WPF control associated by its default Template which provides default appearance of control and behavior, using Control Template the visual appearance and visual behavior of WPF control can be customized and you can create your own control as well using WPF Control Template.

DataTrigger

WPF Data Trigger gets executed when bound data gets changes and meets the condition. For example lets say there are two TextBox textBox and textBox1. The Text property of textBox1 is bound with Text property of textBox. When the textBox text is red then the background color of textBox1 should be red and when black then background color should be black. This can be achieved through DataTrigger.


No comments: