mardi 14 juillet 2015

Checkbox in listview - Checkbox click marks the listview line

I have a listview with one collum like this:

<ListView.View>
   <GridView>
      <GridViewColumn x:Name="OnOffSwitch" Header="Enabled" Width="118">
          <GridViewColumn.CellTemplate>
              <DataTemplate>
                  <CheckBox Click="chkb_Click">
                      <CheckBox.Style>
                          <Style TargetType="CheckBox">
                              <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="true">
                                        <Setter Property="Cursor" Value="Hand" />
                                    </Trigger>
                              </Style.Triggers>
                           </Style>
                      </CheckBox.Style>
                  </CheckBox>
               </DataTemplate>
          </GridViewColumn.CellTemplate>
      </GridViewColumn>
   </GridView>
</ListView.View>

and the click event like this:

private void chkb_Click(object sender, RoutedEventArgs e)
    {
        startSomething = myList.SelectedItem;
    }

My problem is when I don't have a line "marked" in the listview and press the checkbox nothing happends because I don't have a selectedItem. Or if I have several lines, marks line nr 1 and try to press the checkbox on line nr 2. It starts the wrong line then the one i pressed.

The easiest fix I can think of is make sure that when I press the checkbox I want it to mark the line that the checkbox I pressed are on. But I am having problems doing so.

Can anyone please help?




Aucun commentaire:

Enregistrer un commentaire