views:

33

answers:

2
+1  A: 

Here is a basic description of a way you can achieve this. This is not an ideal solution, but should get you on the right track.

  1. Create a field variable in your Tables class that represents a single row in your datatable.
  2. Create a method in your Tables class that sets that field variable to the proper row in your table.
  3. Create properties that expose the values of the row.
  4. Step #2's method will need to do change notification for all the properties exposed in Step #3 (OnPropertyChanged).
  5. Handle the selection_changed event in your code behind, and from that event handler call the method in your Tables class that sets the selected row.
  6. Bind your text blocks to the properties exposed in Step #3.
Phil Sandler
Thanks Phil, it worked perfectly well. I will paste the updated code in my original post. It might be helpful to someone else.
Abhi
A: 

Here is something like that :

public partial class DynamicListViewWindow : Window
{
    public DynamicListViewWindow()
    {
        InitializeComponent();

        ObservableCollection<Person> personList = new ObservableCollection<Person>();

        personList.Add(new Person() { FirstName = "John", LastName = "Doe", Age = 30, Address = "123 Doe Street", Phone = "111-111-1111" });
        personList.Add(new Person() { FirstName = "Jane", LastName = "Doe", Age = 28, Address = "123 Doe Street", Phone = "222-222-2222" });
        personList.Add(new Person() { FirstName = "Mark", LastName = "Doe", Age = 15, Address = "123 Doe Street", Phone = "333-333-3333" });
        personList.Add(new Person() { FirstName = "John", LastName = "Smith", Age = 40, Address = "123 Doe Street", Phone = "444-444-4444" });
        personList.Add(new Person() { FirstName = "Rosy", LastName = "Smith", Age = 36, Address = "123 Doe Street", Phone = "555-555-5555" });

        PersonListView.ItemsSource = personList;
    }

    private void PersonListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (PersonListView.SelectedIndex >= 0)
        {
            Object data = PersonListView.SelectedItem;
            PropertyInfo[] properties = data.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            ExtraPropertiesPanel.Children.Clear();

            foreach (PropertyInfo prop in properties)
            {
                TextBox tb = new TextBox();
                Binding b = new Binding() { Source = data, Path = new PropertyPath(prop.Name) };
                tb.SetBinding(TextBox.TextProperty, b);
                ExtraPropertiesPanel.Children.Add(tb);
            }
        }
    }
}

public class Person
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public Int32 Age { get; set; }
    public String Address { get; set; }
    public String Phone { get; set; }
}

XAML

<Window x:Class="WPFApplication1.DynamicListViewWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window"
    Height="300"
    Width="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0">
        <ListView Name="PersonListView" SelectionChanged="PersonListView_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name"
                                    DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="Last Name"
                                    DisplayMemberBinding="{Binding LastName}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Border>
    <Border Grid.Column="1">
        <StackPanel Name="ExtraPropertiesPanel"></StackPanel>
    </Border>
</Grid>

decyclone