Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can bind ListBox to once specific property say Name &amp; then bind TextBlock to this listbox. Depending upon your need, you can have individual TextBlocks for each property or a single TextBlock... as shown below...</p> <pre><code>&lt;Window x:Class="ListBoxDataBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:ListBoxDataBinding" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;c:EmployeeConverter x:Key="myEmployeeConverter"/&gt; &lt;/Window.Resources&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;ListBox x:Name="lbEmployee"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Label Content="{Binding Name}" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition/&gt; &lt;ColumnDefinition/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Row="0" Grid.Column="0" Width="248" Height="24" Text="Employee Name" /&gt; &lt;TextBlock Grid.Row="0" Grid.Column="1" Width="248" Height="24" Text="{Binding ElementName=lbEmployee, Path=SelectedItem.Name}" /&gt; &lt;TextBlock Grid.Row="1" Grid.Column="0" Width="248" Height="24" Text="Employee EmployeeId" /&gt; &lt;TextBlock Grid.Row="1" Grid.Column="1" Width="248" Height="24" Text="{Binding ElementName=lbEmployee, Path=SelectedItem.EmployeeId}" /&gt; &lt;TextBlock Grid.Row="2" Grid.Column="0" Width="248" Height="24" Text="Employee Destination" /&gt; &lt;TextBlock Grid.Row="2" Grid.Column="2" Width="248" Height="24" Text="{Binding ElementName=lbEmployee, Path=SelectedItem.Destination}" /&gt; &lt;TextBlock Grid.Row="3" Grid.Column="0" Width="248" Height="24" Text="Employee Details" /&gt; &lt;TextBlock Grid.Row="3" Grid.Column="2" Width="248" Height="24"&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding Converter="{StaticResource myEmployeeConverter}" ConverterParameter="FormatEmployee"&gt; &lt;Binding ElementName="lbEmployee" Path="SelectedItem.Name" /&gt; &lt;Binding ElementName="lbEmployee" Path="SelectedItem.EmployeeId" /&gt; &lt;Binding ElementName="lbEmployee" Path="SelectedItem.Destination" /&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;/Grid&gt; &lt;/StackPanel&gt; </code></pre> <p></p> <p>Code behind....</p> <pre><code>public partial class MainWindow : Window { private List&lt;Employee&gt; _lstEmployees; public MainWindow() { InitializeComponent(); Loaded += new RoutedEventHandler(MainWindow_Loaded); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { _lstEmployees = new List&lt;Employee&gt;(); _lstEmployees.Add(new Employee { Name = "Goutham", EmployeeId = "da", Destination = "dar" }); _lstEmployees.Add(new Employee { Name = "Adam", EmployeeId = "dwa", Destination = "ads" }); _lstEmployees.Add(new Employee { Name = "dwad", EmployeeId = "daw", Destination = "wadwa" }); lbEmployee.ItemsSource = _lstEmployees; } } public class EmployeeConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { string name; switch ((string)parameter) { case "FormatEmployee": if (values[0] is String) { name = values[0] + ", " + values[1] + ", " + values[2]; } else { name = String.Empty; } break; default: name = String.Empty; break; } return name; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { string[] splitValues = ((string)value).Split(' '); return splitValues; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload