框绑定的复选框来器isChecked一个场和IsSelected

框绑定的复选框来器isChecked一个场和IsSelected

本文介绍了C#WPF列表框绑定的复选框来器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>



所以,这给了我点击复选框(选中或取消选中)的结果,它会选择该项目。问题是现在经过现场是不是当我添加的项目的约束。

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.

我的问题是你如何能得到复选框既绑定到经过区域仍然有IsSelected工作?我花了几个小时试图用没有运气的不同方式。

My question is how can you get the checkbox to be both bound to the Checked field AND still have the IsSelected work? I've spent hours trying different ways with no luck.

和帮助将是巨大的赞赏。预先感谢您的宝贵时间。

And help would be great appreciated. Thank you in advance for your time

推荐答案

好吧,我回答我自己的问题(也有可能更好地做到这一点这样的感觉树添加)我添加了一个Click事件像这样

Ok, I answered my own question (and there might better to do this so feel tree 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>



,然后加为Click事件

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列表框绑定的复选框来器isChecked一个场和IsSelected?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:45