1
我有一個程序,其中包含一個DataGrid,其中一個DataGridTemplateColumn填充了按鈕。由於行數是可變的,所有按鈕的命令綁定到相同的功能。但是,這意味着我現在需要發送一個CommandParameter來標識按鈕的「索引」(等於它所在的行的索引)。這是我無法弄清楚的。我應該發送什麼作爲CommandParameter以瞭解按鈕的索引?在DataGrid中查找按鈕行
請參閱下面的.xaml(刪除與手頭問題無關的所有內容)。該Button CommandParameter
是什麼,我試圖讓我們填寫。
<UserControl x:Class="WPF.NOptionsTab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
xmlns:l="clr-namespace:WPF">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="CablesColumn">
<Button Command="{Binding Path=DataContext.CablesClick, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding }"
Width="50"
ToolTip="Set cables pulled in this phase."
Content="Set"/>
</DataTemplate>
</Grid.Resources>
<DataGrid x:Name="PhaseGrid"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Cables" CellTemplate="{StaticResource CablesColumn}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
[MSDN - DataGrid - Selected Index](http://msdn.microsoft.com/zh-cn/library/system.windows.controls.primitives.selector.selectedindex.aspx)HTH – XAMlMAX
您可以使用選定的行或者假設您必須在按下按鈕之前選擇行/將按鈕從網格中移出並且僅在選擇了某些內容時啓用。 然後,您可以將選定的行綁定到您的視圖模型,並將其用於該方法中? – Chris
你不需要索引或類似的東西。你現在的代碼很好。使用'CommandParameter =「{Binding}」'將相關數據項(數據行)發送到命令。該命令應該使用數據項而不是索引或其他不相關的信息。 –