How to use LongBindingPath in WPF


This article will describe for you how to use LongBindingPath using WPF. This is small example but very useful in WPF. 

First of all make a new WPF application and add a new page using "Add New Item" then drag and drop some controls onto page. I have added one list box with a few listboxitems and I have added a textbox and lables. When we select any item of listbox the selected value will show in label.

Content="{Binding ElementName=TestPage, Path=Content.Children[1].SelectedItem.Content}">

Here is my full .xaml code

<Page x:Class="WPFBinding.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d" FontSize="14pt" Name="TestPage">
    <StackPanel>
        <TextBlock Name="textBlock1" Text="First element in stack panel" HorizontalAlignment="Center" />
        <ListBox Name="listBox1" HorizontalAlignment="Center" Margin="24" Width="208" Height="132">
            <ListBoxItem Content="Raj Kumar Beniwal" />
            <ListBoxItem Content="Jeniffer Tapagia jenny" />
            <ListBoxItem Content="Kishan Kant Kahu" />
            <ListBoxItem Content="Sama Sikandar Jetli" />
            <ListBoxItem Content="Jeenat Chatterjee" />
        </ListBox>
        <TextBlock  Name="textBlock2" HorizontalAlignment="Center">
            <Label Content="Number of character in fourth list box item = "></Label>
            <Label Content="{Binding ElementName=TestPage, Path=Content.Children[1].Items[3].Content.Length}" Name="label1" />
            <LineBreak></LineBreak>
            <Label Content="Number of character in selected item = "></Label>
            <Label Content="{Binding ElementName=TestPage, Path=Content.Children[1].SelectedItem.Content.Length}"></Label>
            <LineBreak></LineBreak>
            <Label Content="Selected Item Text = "></Label>
            <Label Content="{Binding ElementName=TestPage, Path=Content.Children[1].SelectedItem.Content}"></Label>
        </TextBlock>
    </StackPanel>
</Page>

So when you run the application.

Image1.jpg
 
Image 1.

When you select and listitem then result should be like this.

<Label Content="Number of character in selected item = "></Label>
<Label Content="{Binding ElementName=TestPage, Path=Content.Children[1].SelectedItem.Content.Length}"></Label>
            <LineBreak></LineBreak>
 <Label Content="Selected Item Text = "></Label>
<Label Content="{Binding ElementName=TestPage, Path=Content.Children[1].SelectedItem.Content}"></Label>

Image2.jpg

Image 2.

Let's discuss one more important thing, how to get day of the week in WPF.

Add this reference.

xmlns:mscorlib="clr-namespace:System.Globalization;assembly=mscorlib"

<ListBox Height="156" HorizontalAlignment="Center" Margin="76,55,0,0"
                 Name="listBox1" VerticalAlignment="Top"
                 Width="120"
                 ItemsSource="{Binding Source={x:Static mscorlib:DateTimeFormatInfo.CurrentInfo}, Path=DayNames, Mode=OneTime}" />
        <TextBlock Height="23" HorizontalAlignment="Center"
                   Margin="128,228,52,0" Name="textBlock1"
                   Text="{Binding ElementName=listBox1, Path=SelectedItem, Mode=OneWay}" VerticalAlignment="Top" Width="120" />

Result should be like this.

Image3.jpg 

Image 3.

When you select any day then

Image4.jpg
 
Image 4.