源地址:http://zhidao.baidu.com/question/400826602.html
第一种:
其实这个可以通过绑定父控件的属性来实现,给个Demo
前台:
<Grid>
<ListBox>
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Button Content="click" Margin="0,2" Width="50" Height="25" Click="Button_Click"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<ListBoxItem Tag="1"/>
<ListBoxItem Tag="2"/>
<ListBoxItem Tag="3"/>
</ListBox>
</Grid>
后台:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
rs.AncestorType=typeof(ListBoxItem);
Binding binding = new Binding("Tag") { RelativeSource=rs};
btn.SetBinding(Button.ContentProperty,binding);
}
第二种:
ListBoxItem本身就是可选的,你要操作的话,可以给ListBoxItem的Selected属性添加事件,SelectedIndex值里面存了Item的序号的,值从0开始的。