<tfoot id='fICJf'></tfoot>
      <i id='fICJf'><tr id='fICJf'><dt id='fICJf'><q id='fICJf'><span id='fICJf'><b id='fICJf'><form id='fICJf'><ins id='fICJf'></ins><ul id='fICJf'></ul><sub id='fICJf'></sub></form><legend id='fICJf'></legend><bdo id='fICJf'><pre id='fICJf'><center id='fICJf'></center></pre></bdo></b><th id='fICJf'></th></span></q></dt></tr></i><div id='fICJf'><tfoot id='fICJf'></tfoot><dl id='fICJf'><fieldset id='fICJf'></fieldset></dl></div>
      1. <legend id='fICJf'><style id='fICJf'><dir id='fICJf'><q id='fICJf'></q></dir></style></legend>

      2. <small id='fICJf'></small><noframes id='fICJf'>

          <bdo id='fICJf'></bdo><ul id='fICJf'></ul>

        C# WPF ListBox Checkbox 绑定 IsChecked 到一个字段和 IsSelected?

        C# WPF ListBox Checkbox Binding IsChecked to a Field and IsSelected?(C# WPF ListBox Checkbox 绑定 IsChecked 到一个字段和 IsSelected?)
        1. <i id='nxdkB'><tr id='nxdkB'><dt id='nxdkB'><q id='nxdkB'><span id='nxdkB'><b id='nxdkB'><form id='nxdkB'><ins id='nxdkB'></ins><ul id='nxdkB'></ul><sub id='nxdkB'></sub></form><legend id='nxdkB'></legend><bdo id='nxdkB'><pre id='nxdkB'><center id='nxdkB'></center></pre></bdo></b><th id='nxdkB'></th></span></q></dt></tr></i><div id='nxdkB'><tfoot id='nxdkB'></tfoot><dl id='nxdkB'><fieldset id='nxdkB'></fieldset></dl></div>

        2. <tfoot id='nxdkB'></tfoot>
          <legend id='nxdkB'><style id='nxdkB'><dir id='nxdkB'><q id='nxdkB'></q></dir></style></legend>

              <tbody id='nxdkB'></tbody>
                <bdo id='nxdkB'></bdo><ul id='nxdkB'></ul>

                1. <small id='nxdkB'></small><noframes id='nxdkB'>

                  本文介绍了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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                    <legend id='fEeP1'><style id='fEeP1'><dir id='fEeP1'><q id='fEeP1'></q></dir></style></legend>

                        <tbody id='fEeP1'></tbody>
                      <i id='fEeP1'><tr id='fEeP1'><dt id='fEeP1'><q id='fEeP1'><span id='fEeP1'><b id='fEeP1'><form id='fEeP1'><ins id='fEeP1'></ins><ul id='fEeP1'></ul><sub id='fEeP1'></sub></form><legend id='fEeP1'></legend><bdo id='fEeP1'><pre id='fEeP1'><center id='fEeP1'></center></pre></bdo></b><th id='fEeP1'></th></span></q></dt></tr></i><div id='fEeP1'><tfoot id='fEeP1'></tfoot><dl id='fEeP1'><fieldset id='fEeP1'></fieldset></dl></div>
                      • <tfoot id='fEeP1'></tfoot>
                      • <small id='fEeP1'></small><noframes id='fEeP1'>

                            <bdo id='fEeP1'></bdo><ul id='fEeP1'></ul>