开发者

ListBox and finding the selected checkbox

I am currently working on a C# wpf project. I have a listbox and I am dynamically adding checkboxes to the listbox wi开发者_运维百科th the following code.

while (reader.Read())
{
     Console.WriteLine("Database: " + reader.GetString("Database"));
     string databaseName = reader.GetString("Database");
     CheckBox chkDatabase = new CheckBox();
     chkDatabase.Content = databaseName.Replace("_", "__");
     chkDatabase.Uid = "chk_" + reader.GetString("Database");
     chkDatabase.Checked += new RoutedEventHandler(chkDatabase_Checked);

     lstDatabase.Items.Add(chkDatabase);
}

This is working fine and I the routedeventhandler works fine to determine when a check box has been selected or not.

What I want to be able to do is to allow the user to click on the row that the checkbox is in, instead of actually checking the row. I've added an event handler to the list box for the selection changed like the following:

private void lstDatabase_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Console.WriteLine("Selection Changed");
    ListBox checkbox = (ListBox)e.Source;
    Console.WriteLine("Checkbox2: " + checkbox.SelectedValue);
}

How can I get the checkbox value from the selection changed event handler.

Thanks for any help you can provide.


To get the checkbox itself we simply cast the selected item (which will be a checkbox as you have only added checkboxes to the listbox's item) to checkbox.

CheckBox chkBox = lstDatabase.SelectedItem as CheckBox;

Then we simply get the checkvalue using

chkBox.IsChecked;

Put that code inside you SelectionChanged function and you'll retrieve the checkbox value. You can also set it there too.

I hope this helps.

EDIT:

However I would suggest running this code on a different event. If a user clicks on the already selected item in order to toggle the checkbox, a SelectionChanged event will NOT fire. I would suggest MouseUp, provided you test that there is actually a selectedItem before running the code.


One simple way of doing it is this:

ListBoxItem lbItem = new ListBoxItem();
lbItem.Content = chkDatabase;
lstDatabase.Items.Add(lbItem);

Then in your handler:

bool chkVal = false;
ListBoxItem selItem = lstDatabase.SelectedItem as ListBoxItem;
if (selItem != null && selItem.Content is CheckBox)
    chkVal = ((CheckBox)selItem.Content).IsChecked;


A way of solving this is by switching to databinding.

Create a class (let's call it A) (which implements INotifyPropertyChanged) that represents a single item in the listbox and add a property to the class called Selected.

Create an instance of ObservableCollection (let's name it col) and add an instance of A for each row/item.

Now bind the ListBox as follows:

First in code lstDatabase.DataContext = col;

Then in XAML:

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

(I didn't use a compiler to check this)

Now if you want to know what items are selected just make this selection on the collection:

var selectedItems = col.Where(item => item.IsSelected);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜