在数据绑定列表视图中使用Button单击相同索引更改TextBox的字段

问题描述 投票:0回答:1

我有一个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中的值放在同一个索引处呢?

c# uwp
1个回答
0
投票

您可以使用“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'
}

我希望它对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.