从列表框wpf中获取选中的复选框标记值 [英] Get selected checkbox tag value from Listbox wpf
本文介绍了从列表框wpf中获取选中的复选框标记值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好开发者,
我正在使用WPF应用程序,我有一个列表框,语法如下。
< Window.Resources >
< DataTemplate x:密钥 = ListBoxItemTemplate >
<! - < WrapPanel> - - >
< CheckBox N. ame = CheckBoxZone 内容 = {Binding SoilStatus} 保证金 = 3,10 标记 = {Binding ID} / >
<! - < / WrapPanel> - >
< / DataTemplate >
< / Window.Resources >
< ListBox 网格.Row = 0 Grid.Column = 1 保证金 = 3 10 名称 = lstSoilStatus ItemTemplate = {DynamicResource ListBoxItemTemplate} ScrollViewer.HorizontalScrollBarVisibility = 已禁用 >
< ListBox.ItemsPanel >
< < span class =code-leadattribute> ItemsPanelTemplate >
< WrapPanel 方向 = Horizon tal > < / WrapPanel >
< / ItemsPanelTemplate >
< /ListBox.ItemsPanel >
< / ListBox >
我从代码绑定此值,以下是要绑定的代码:
string query =SELECT ID,SoilStatus FROM SoilStatus ORDER BY SoilStatus;
DBOperation obj = new DBOperation();
DataTable dt = obj.GetData(query);
lstSoilStatus.ItemsSource = dt.AsDataView();
我的代码按预期工作。
现在我选择了多个复选框作为我的要求。
我想在c#代码中获取所选复选框的标签值。
如何实现这一目标?
有什么想法?
解决方案
你可以试试使用包含对象的类(绑定列表的对象)和检查/取消选中的布尔值。然后,您可以遍历该集合并查找已检查的条目。哦,记得将CheckBox的IsChecked属性绑定到你的值。
希望这会有所帮助。
PS:你可以试试这个: http://merill.net/2009/10/wpf-checked-listbox /
我把答案放在这里是为了告诉其他人它有一个解决方案。 : - )
Hello Developer,
I am working with WPF application where I have a Listbox with following syntax.
<Window.Resources>
<DataTemplate x:Key="ListBoxItemTemplate" >
<!--<WrapPanel>-->
<CheckBox Name="CheckBoxZone" Content="{Binding SoilStatus}" Margin="3, 10" Tag="{Binding ID}" />
<!--</WrapPanel>-->
</DataTemplate>
</Window.Resources>
<ListBox Grid.Row="0" Grid.Column="1" Margin="3 10" Name="lstSoilStatus" ItemTemplate="{DynamicResource ListBoxItemTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"></WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
I am binding this value from code, following is the code to bind:
string query = "SELECT ID, SoilStatus FROM SoilStatus ORDER BY SoilStatus"; DBOperation obj = new DBOperation(); DataTable dt = obj.GetData(query); lstSoilStatus.ItemsSource = dt.AsDataView();
My code is working as expected.
Now I am selecting multiple checkbox as my requirement.
And I want to get the tag value of that selected checkbox in c# code.
How can I achieve this?
Any idea?
解决方案
Hi,
You may try to use a class that contains your object (the one you bind the list from) and a boolean value which is checked/unchecked. Then you can iterate through that collection and find the checked entries. Oh, remember to bind the IsChecked property of the CheckBox to your value.
Hope this helps.
PS: you may try this: http://merill.net/2009/10/wpf-checked-listbox/
I put the answer here in order to tell other that it has a solution. :-)
这篇关于从列表框wpf中获取选中的复选框标记值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文