我有一个ListView
,其中有一个集合作为ItemsSource
。
<ListView x:Name="lvBT" Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}"
ItemsSource="{x:Bind ViewModel.CurrentPoste.TableauxBT}" Margin="0,0,0,12"
IsEnabled="{x:Bind ViewModel.CurrentPoste.BtEdition, Mode=TwoWay}"
SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:BT">
[...]
<TextBox x:Name="tbNumSerieBT" HorizontalAlignment="Stretch" Margin="12,32,16,0" Text="{x:Bind NumSerie, Mode=TwoWay}" VerticalAlignment="Top" Grid.Column="3" FontSize="16" Grid.ColumnSpan="2"/>
<Button x:Name="bScannerBT" Grid.Column="5" HorizontalAlignment="Stretch" Margin="12,32,15,0" VerticalAlignment="Top" Content="Scanner tabeau BT" FontSize="14" Click="BScannerBT_Click"/>
[...]
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
tableauxBT
表示BT
对象的集合,对于该集合中的每个对象,我正在创建一个 DataTemplate,例如基本数据绑定。
当我单击该模板中的Button
时,我正在扫描带有BarcodeScanner
的条形码,并希望将返回值放在TextBox
字段中。
对于每个不同BT
项目的每个按钮,我想扫描不同的条形码,但问题是我不知道如何获取单击以将值放入正确TextBox
的Button
的索引。
那么我该怎么做才能获取点击Button
的索引,以将TextBox
中的值放在同一个索引处呢?
您可以使用
"BScannerBT_Click"方法获取单击按钮的索引。
public BScannerBT_Click(object sender, EventArgs e)
{
var myClickedButton = (Button)sender; //this object hold all information you need.
//You can reach the button's dataContext and change the value you want to.
var buttonDataContext = myClickedButton.DataContext.
//now you have the ViewModel (buttonDataContext) associated with the 'NumSerie'
}
我希望它对你有所帮助。