参考:wpf中给listview绑定数据并在listview的每一行中添加一个按钮,通过单击按钮获得按钮所在行的数据
在 GotFocus 事件中进行处理
<ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> <Ellipse Width="10" Height="10" Fill="Red" Margin="0,0,5,0"></Ellipse> <StackPanel Orientation="Vertical" VerticalAlignment="Center"> <TextBlock Text="{Binding Date,StringFormat={}{0:yyyy-MM-dd}}" FontSize="15" Margin="2,2,0,5"/> <TextBox Text="{Binding Description,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Foreground="Gray" BorderThickness="0" Background="Transparent" GotFocus="TextBox_GotFocus"/> </StackPanel> </StackPanel> </DataTemplate> </ListBox.ItemTemplate>
private void TextBox_GotFocus(object sender, RoutedEventArgs e) { TextBox txtBox = sender as TextBox; Model.RevisionData c = txtBox.DataContext as Model.RevisionData; listbox.SelectedItem = c; }