C# WPF ListBox Checkbox 绑定 IsChecked 到一个字段和 IsSelected? [英] C# WPF ListBox Checkbox Binding IsChecked to a Field and IsSelected?

查看:103
本文介绍了C# WPF ListBox Checkbox 绑定 IsChecked 到一个字段和 IsSelected?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 CheckBox 绑定到字段,但同时也会触发复选框的 IsSelected.

I'm trying to bind a CheckBox to a field but also trigger the checkbox's IsSelected.

这是与数据绑定一起使用的 ListBox 设置

Here is the ListBox setup that is working with the Binding to data

<ListBox x:Name="lstExclude"  Grid.Column="2" SelectionMode="Single" >
  <ListBox.ItemTemplate>
    <DataTemplate>
      <CheckBox  Content="{Binding Text}" 
          IsChecked="{Binding Checked ,Mode=TwoWay}"/>
      </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

这里是绑定相关的代码

public MainWindow()
{
    InitializeComponent();

    List<CheckBoxListItem> items1 = new List<CheckBoxListItem>();
    items1.Add(new CheckBoxListItem(true, "home"));
    items1.Add(new CheckBoxListItem(false, "work"));
    items1.Add(new CheckBoxListItem(true, "cell"));
    lstExclude.ItemsSource = items1;
}

public class CheckBoxListItem
{
   public bool Checked { get; set; }
   public string Text { get; set; }

   public CheckBoxListItem(bool ch, string text)
   {
     Checked = ch;
     Text = text;
    }
}

这会正确绑定复选框选中值,但是如果我单击复选框(选中或未选中),我希望它选择项目,所以我尝试这样做

This binds the checkbox checked value correctly, but if I click the checkbox (checked or unchecked), I want it to select the item, so I tried doing it this way

<ListBox x:Name="lstExclude"  Grid.Column="2" SelectionMode="Single" >
  <ListBox.ItemTemplate>
    <DataTemplate>
      <CheckBox  Content="{Binding Text}" 
          IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}"/>
      </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

所以这给了我单击复选框(选中或取消选中)的结果,它将选择该项目.现在的问题是,当我添加项目时,Checked 字段未绑定.

So this gives me the results of clicking the checkbox (check or uncheck) and it will select the item. The problem is now the Checked field is not bound when I add the items.

如何让复选框既绑定到 Checked 字段又让 IsSelected 工作?

How can you get the checkbox to be both bound to the Checked field AND still have the IsSelected work?

推荐答案

好的,我已经回答了我自己的问题(可能会有更好的做法,所以请随意添加)我像这样在复选框中添加了一个 Click 事件

Ok, I answered my own question (and there might better to do this so feel free to add) I added a Click event to the checkbox like so

<ListBox x:Name="lstExclude"  Grid.Column="2" SelectionMode="Single" >
 <ListBox.ItemTemplate>
    <DataTemplate>
      <CheckBox  Content="{Binding Text}" 
          IsChecked="{Binding Checked ,Mode=TwoWay}" Click="CheckBox_Click"/>
      </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

然后为点击事件添加此代码

and then added this code for the Click Event

private void CheckBox_Click(object sender, RoutedEventArgs e)
{
    var cb = sender as CheckBox;
    var item = cb.DataContext;
    lstExclude.SelectedItem = item;
}

现在,当您单击复选框(选中或未选中)时,该项目被选中,并且该项目可用于lstExclude.SelectedIndex"方法

Now the item gets selected when you click the checkbox (checked or unchecked) and the item is available to the 'lstExclude.SelectedIndex' method

我希望这可以帮助任何遇到同样问题的人.

I hope this helps anybody coming along with the same problem.

这篇关于C# WPF ListBox Checkbox 绑定 IsChecked 到一个字段和 IsSelected?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆