开发者

WPF: Checkbox in a ListView/Gridview--How to Get ListItem in Checked/Unchecked Event?

开发者 https://www.devze.com 2022-12-27 13:12 出处:网络
In the code behind\'s CheckBox_Checked and CheckBox_Unchecked events, I\'d like to be able to access the item in MyList that the checkbox is bound to.

In the code behind's CheckBox_Checked and CheckBox_Unchecked events, I'd like to be able to access the item in MyList that the checkbox is bound to.

Is there an easy way to do this?

<ListView ItemsSource="{Binding Path=MyList, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" MinHeight="100" MaxHeight="100">
  <ListView.View>
    <GridView>
      <GridViewColumn>                            
        <GridViewColumn.CellTemplate>
          <DataTempl开发者_如何学Goate>
            <CheckBox Margin="-4,0,-4,0" IsChecked="{Binding MyBoolProperty}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>


The CheckBox.DataContext property will hold the bound item.


Do you want to get access to the object that the CheckBox is bound to, or the actual ListBoxItem that is used in the UI? If you just want the object the CheckBox is bound to, then @Charlie 's answer will work. Otherwise, you can call VisualTreeHelper.GetParent() until you reach the ListBoxItem.

0

精彩评论

暂无评论...
验证码 换一张
取 消