views:

644

answers:

1

Hi!

Enough is enough - I've just spent an hour searching around trying to find out how to read the ListViewSubItem values (if that's correct terminology in XAML) from a ListView. Here's a little ListView:

<ListView x:Name="CreatableAccounts" ItemsSource="{Binding Processable}" Margin="10,0">
        <ListView.View>
              <GridView>
                    <GridViewColumn Header="Site Name" DisplayMemberBinding="{Binding SiteName}"/>
                    <GridViewColumn Header="From Package" DisplayMemberBinding="{Binding FiCodeDLL.Name}"/>
                </GridView>
         </ListView.View>
</ListView>

and here's my attempt to read the values which is clearly not going to work:

private void CreateAccounts_Click(object sender, RoutedEventArgs e)
    {
        ListViewItem selected = CreatableAccounts.SelectedItem;
        selected.Ite //  no Items, Text or similar property


    }

Can anyone point me in the right direction? Grazie in advance for your help!

+2  A: 

Hi,

The ListView has a dependency property "SelectedItem" to which you can bind an instance of your collection child item, thus:

<DockPanel>
    <Button DockPanel.Dock="Top" Click="Button_Click">Selected Item</Button>
    <ListView ItemsSource="{Binding AllItems}" SelectedItem="{Binding SelectedItem}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
            </GridView>
        </ListView.View>
    </ListView>
</DockPanel>

Now you can create a ViewModel that exposes an ObservableCollection filled with your items, and a single instance of an item which is your SelectedItem...

something like:

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

        WindowViewModel vm = new WindowViewModel();
        this.DataContext = vm;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowViewModel vm = this.DataContext as WindowViewModel;
        MessageBox.Show(vm.SelectedItem.Name);
    }

}

public class WindowViewModel
{
    public WindowViewModel()
    {
        AllItems = new ObservableCollection<Person>();
        AllItems.Add(new Person { Name = "Joe", Age = 26 });
        AllItems.Add(new Person { Name = "Mary", Age = 23 });
        AllItems.Add(new Person { Name = "Bill", Age = 32 });
        AllItems.Add(new Person { Name = "Harry", Age = 36 });
        AllItems.Add(new Person { Name = "Julie", Age = 18 });
        AllItems.Add(new Person { Name = "Claire", Age = 42 });
    }

    public ObservableCollection<Person> AllItems { get; set; }

    private Person _selectedItem;
    public Person SelectedItem
    {
        get { return _selectedItem; }
        set { _selectedItem = value; }
    }
}

public class Person : INotifyPropertyChanged
{

    private string _name;
    private int _age;

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
    }

    public int Age
    {
        get { return _age; }
        set
        {
            if (_age != value)
            {
                _age = value;
                RaisePropertyChanged("Age");
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

Hope this helps :)

Ian

IanR
OK - so now I feel sheepish... I've just re-read your original question and realise that you already knew about the SelectedItem property... seems the only bit you were missing was the fact that you can cast it to be an instance of your child object, so instead of:ListViewItem selected = CreatableAccounts.SelectedItem;you could have saidChildObject selected = CreatableAccounts.SelectedItem as ChildObject;ho hum...
IanR
Don't worry Ian, I learnt a lot from your code example and it got me to study the topic seriously, which it clearly requires. I'm now not getting the list view to populate for some reason so I'm running through the tutorial on MSDN here, which seems pretty thorough: http://msdn.microsoft.com/en-us/library/ms752347.aspxGrazie!Gregg
Gregg Cleland
you're most welcome :)
IanR