![](/img/trans.png)
[英]Getting the row on the button_click event inside a DataGrid column in WPF
[英]Wpf getting the row number of DataGrid on button click even C#
我在DataGrid的每一行中都有2個按鈕。 它們將用於保存編輯后的行信息,或者用於刪除行。 但是我無法在點擊事件中檢索行號。 (我的stackoverflow也有類似的問題,但對我沒有作用)
<DataTemplate x:Key="myTemplate">
<StackPanel Orientation="Horizontal">
<Button Height="21" Width="21" Margin="4" Style="{StaticResource InformButton}" Click="ClientSave">
<StackPanel Height="17" Width="20">
<Image Source="pack://application:,,,/TimeCounter;component/resources/save.png" Height="14" Width="14" Margin="-1,0" HorizontalAlignment="Left"/>
</StackPanel>
</Button>
<Button Height="21" Width="21" HorizontalAlignment="Right" Margin="4" Style="{StaticResource InformButton}">
<StackPanel Height="18" Width="21">
<Image Source="pack://application:,,,/TimeCounter;component/resources/delete.png" Height="14" Width="13" Margin="0 ,0" HorizontalAlignment="Left"/>
</StackPanel>
</Button>
</StackPanel>
</DataTemplate>
如果卡在事件上,我將使用綁定來檢索數據項及其索引。
在事件處理程序中:
var btn = sender as Button;
var item = btn.GetBindingExpression(Button.DataContextProperty).DataItem as YourRowItemClass;
var index = YourViewModel.ItemCollection.IndexOf(item);
查看模型:
class YourViewModel : INotifyPropertyChanged {
ObservableCollection<RowItem> ItemCollection { get; private set; }
}
在您的按鈕XAML中:
DataContext="{Binding}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.