在WPF中获取列表视图的复选框值

发布于 2024-12-21 18:56:53 字数 767 浏览 7 评论 0原文

我有这样的代码

<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>

,在其中我将在按钮单击事件中从数据库动态绑定复选框的值,但我无法获取列表视图的选中复选框的值。

请帮助我解决这个问题。提前致谢

I am having the code like

<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>

In which I will bind the value for checkbox dynamically from db at buttonclick event I can't able to get the value of checked checkbox of listview.

Please help me regarding this. Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-12-28 18:56:53

不要尝试从 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;

Do not try to get the checked value from the UI. Use the IsPersonChecked property from the data object.

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

EDIT

After understanding that you used a DataView your query would be something like this:

var dataView = listView1.DataContext as DataView;
var selectedPersonRowsQuery = from row in dataView
                              where row["IsPersonChecked"]
                              select row;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文