WPF - How to read values from checkbox items that are inside listbox item and bindied with database?

无人久伴 提交于 2019-12-12 02:46:11

问题


I am building an app that will return data from database based on the items that are checked on check box. I have successfully displayed data from database by binding the data. Like this:

XAML:

<ListBox x:Name="listBox1" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="52" Margin="141,264,0,0" VerticalAlignment="Top" Width="307" SelectionMode="Multiple">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox x:Name="checkBox1" IsChecked="{Binding IsSelected}"  Content="{Binding NacinGrejanja}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

C#:

       private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        Dataset1 ds= new Dataset1 ();
        GrejanjeTableAdapter gta = new GrejanjeTableAdapter();
        gta.Fill(ds.Grejanje);
        listbox1.DataContext = ds.Grejanje;
    }

But I am unable to figure out how to extract string values from checked values and then again search for those values in database. I can't access any of the properties for checkBox1 through code.

Also I would want first to check if any of the checkbox items is checked in the first place.


回答1:


You can use following linq:

ds.Grejanje.Where(item => item.IsSelected == true);

It will return the list of items which are checked.



来源:https://stackoverflow.com/questions/40452039/wpf-how-to-read-values-from-checkbox-items-that-are-inside-listbox-item-and-bi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!