Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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>

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.

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

like image 993
user3573191 Avatar asked Apr 25 '14 14:04

user3573191


Video Answer


4 Answers

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

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.

like image 75
user3573191 Avatar answered Oct 30 '22 15:10

user3573191


 <CheckBox Padding="10" 
           IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type  
                                                 ListBoxItem}}, Path=IsSelected}">
        <CheckBox.LayoutTransform>
                  <ScaleTransform ScaleX="1" ScaleY="1" />
        </CheckBox.LayoutTransform>
  </CheckBox>
like image 34
Ramji Avatar answered Oct 30 '22 13:10

Ramji


Would it work to bind both UI properties to the Checked object model property?

<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>
like image 23
Rachel Avatar answered Oct 30 '22 14:10

Rachel


you can use a MultiBinding with MultiConverter

<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

like image 40
erradi mourad Avatar answered Oct 30 '22 13:10

erradi mourad