简体   繁体   English

C#WPF ListBox复选框绑定IsChecked到字段和IsSelected?

[英]C# WPF ListBox Checkbox Binding IsChecked to a Field and IsSelected?

I'm trying to bind a CheckBox to a field but also trigger the checkbox's IsSelected. 我正在尝试将CheckBox绑定到字段,但也会触发复选框的IsSelected。

Here is the ListBox setup that is working with the Binding to data 下面是使用Binding to data的ListBox设置

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

And here is the code associated with the binding 这是与绑定相关的代码

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. 现在的问题是我添加项目时没有绑定Checked字段。

How can you get the checkbox to be both bound to the Checked field AND still have the IsSelected work? 如何让复选框绑定到Checked字段并且仍然有IsSelected工作?

Would it work to bind both UI properties to the Checked object model property? 是否可以将两个UI属性绑定到Checked对象模型属性?

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsSelected" Value="{Binding Checked, Mode=OneWay}"/>
    </Style>
</ListBox.ItemContainerStyle>

<ListBox.ItemTemplate>
   <DataTemplate>
      <CheckBox Content="{Binding Text}" IsChecked="{Binding Checked}"/>
   </DataTemplate>
</ListBox.ItemTemplate>

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 好的,我回答了我自己的问题(最好这样做,所以随意添加)我在复选框中添加了一个Click事件,如此

<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 然后为Click事件添加了此代码

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

Now the item gets selected when you click the checkbox (checked or unchecked) and the item is available to the 'lstExclude.SelectedIndex' method 现在,当您单击复选框(选中或取消选中)时,项目将被选中,并且该项目可用于'lstExclude.SelectedIndex'方法

I hope this helps anybody coming along with the same problem. 我希望这有助于任何人遇到同样的问题。

you can use a MultiBinding with MultiConverter 您可以使用MultiBindingMultiConverter

<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource YourMultiBindConverter}"> 
                <Binding Path="IsSelected" RelativeSource={RelativeSource AncestorType=ListBoxItem}"/> 
                <Binding Path="Checked"/> 
 </MultiBinding> 
</CheckBox.IsChecked>

and create a YourMultiBindConverter that implement IMultiValueConverter 并创建实现IMul​​tiValueConverter一个YourMultiBindConverter

 <CheckBox Padding="10" 
           IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type  
                                                 ListBoxItem}}, Path=IsSelected}">
        <CheckBox.LayoutTransform>
                  <ScaleTransform ScaleX="1" ScaleY="1" />
        </CheckBox.LayoutTransform>
  </CheckBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM