1

I have a Custom ListBox with multiple columns per one Item

<ListBox Name="UserListBox" Loaded="GetUsers_OnLoad" SelectionChanged="UserSelected">
<ListBox.ItemTemplate>
    <DataTemplate>
        <DockPanel Name="UserDockPanel" Margin="4">
            <TextBlock Name="UsernameTextBlock" Text="{Binding Path=Username}"/>
            <CheckBox Name="OneCheckBox" IsHitTestVisible="False" IsChecked="{Binding One}" />
            <CheckBox Name="TwoCheckBox" IsHitTestVisible="False" IsChecked="{Binding Two}" />
            <CheckBox Name="ThreeCheckBox" IsHitTestVisible="False" IsChecked="{Binding Three}" />
        </DockPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

What I am trying to do is when the user selects an item that I can parse the individual values for that item (UsernameTextBlock, OneCheckbox, TwoCheckBox, ThreeCheckBox).

I have tried selected which throws an error and selection changed seems to work but I do not know how to retrieve the individual values for the item selected.

Any insight would be appreciated.

UPDATE:

Here is the code behind

private void UserSelected(object sender, RoutedEventArgs e)
{
    var userListBox = FindName("UserListBox") as ListBox;

    var selectedItem = userListBox.SelectedItem as ListBoxItem;

    MessageBox.Show(selectedItem.Username);
}

I am currently just showing a message popup to show what I am accessing

UPDATE 2:

private void GetUsers_OnLoad(object sender, RoutedEventArgs e)
{
    _outreachAuths = _outreachTableAdapter.GetOutreachAuths();

    var users = new List<UserItem>();

    foreach (DataRow row in _outreachAuths.Rows)
    {
        users.Add(new UserItem() { Username = row.ItemArray[0].ToString(), One = false, Two = true, Three = ((row.ItemArray[2].ToString() == "1"))});
    }

    var userList = sender as ListBox;
    if (userList != null) userList.ItemsSource = users;
}
4
  • Could you please post the codebehind/ViewModel code that deals with the SelectionChanged event? Commented Apr 7, 2015 at 16:03
  • @goobering I have added the code behind Commented Apr 7, 2015 at 16:15
  • Nearly there with this one. Can you please post the code you use to populate the ListBox with items? Commented Apr 7, 2015 at 17:50
  • @goobering there is the other piece of code. Sorry for the late response. I was in a meeting. Commented Apr 7, 2015 at 19:43

2 Answers 2

1

In your UserSelected handler you're casting the selected item to type ListBoxItem:

var selectedItem = userListBox.SelectedItem as ListBoxItem;

In order to access the properties you're looking for you'll need to cast it to its original type which is, I believe, UserItem.

var selectedItem = userListBox.SelectedItem as UserItem;
Sign up to request clarification or add additional context in comments.

Comments

0

Bind the listbox's SelectedItem property to a property in your view model. You will then have access to the item when it's value changes in the VM.

<ListBox Name="UserListBox" Loaded="GetUsers_OnLoad" SelectionChanged="UserSelected" SelectedItem={Binding Path=PropertyOnViewModel}>

1 Comment

The event handlers on Loaded and SelectionChanged suggest that this is more likely to have a codebehind file rather than a ViewModel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.