使用按钮单击“数据绑定列表视图”中的同一索引来更改文本框的字段



我有一个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项目的每个按钮,我想扫描不同的条形码,但问题是我不知道如何获取单击以将值放入正确TextBoxButton的索引。

那么我该怎么做才能获取点击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'
}

我希望它对你有所帮助。

最新更新