我有一个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
项目的每个按钮,我想扫描不同的条形码,但问题是我不知道如何获得被点击的Button
的索引将值放在正确的TextBox
中。
那么我该如何才能获得点击的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'
}
我希望它对你有所帮助。