WPF - 如何读取列表框项目中和绑定与数据库的复选框项目的值? [英] WPF - How to read values from checkbox items that are inside listbox item and bindied with database?
问题描述
我建立一个应用程式,将根据在复选框上勾选的项目从数据库返回数据。我已通过绑定数据成功显示数据库中的数据。像这样:
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;
}
但我无法弄清楚如何从检查值中提取字符串值,然后再次在数据库中搜索这些值。我不能通过代码访问checkBox1的任何属性。
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.
此外,我想首先检查是否任何复选框项目被选中。 / p>
Also I would want first to check if any of the checkbox items is checked in the first place.
推荐答案
您可以使用以下linq:
You can use following linq:
ds.Grejanje.Where(item => item.IsSelected == true);
它将返回已检查的项目列表。
It will return the list of items which are checked.
这篇关于WPF - 如何读取列表框项目中和绑定与数据库的复选框项目的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!