要在WPF中选择listview的Checkbox值

时间:2011-12-19 09:16:34

标签: wpf checkbox selected

我有像

这样的代码
<ListView ItemsSource="{Binding}"  Height="110.277" Margin="4,0,3,-138" Name="listView1" VerticalAlignment="Bottom">
   <ListBox.ItemTemplate>
      <DataTemplate>

        <!--<TextBlock Text="{Binding Path=Name}" Width="100" />-->
        <!--<CheckBox  IsChecked="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"  Width="100"/>-->
        <CheckBox Name="chk1" Content="{Binding Path=Name}" IsChecked="{Binding IsPersonChecked}" Checked="checked_accname"   Width="100" />

      </DataTemplate>
 </ListBox.ItemTemplate>

我将在buttonclick事件中动态地从db绑定复选框的值,我无法获取listview的checked复选框的值。

请帮我解决这个问题。在此先感谢

1 个答案:

答案 0 :(得分:1)

不要尝试从UI获取选中的值。使用数据对象中的IsPersonChecked属性。

var persons = listView1.DataContext as Persons;
var selectedPersonsQuery = from person in persons
                           where person.IsPersonChecked
                           select person;

修改

在了解您使用DataView后,您的查询将是这样的:

var dataView = listView1.DataContext as DataView;
var selectedPersonRowsQuery = from row in dataView
                              where row["IsPersonChecked"]
                              select row;