C# xamarin表单从按钮单击捕获列表视图
下面的“我的代码”显示一个列表视图,当单击“请播放您的订单”按钮时,按下功能按钮(object sender,EventArgs e)。如何从ButtonPressed函数捕获整个listview 我试过类似的东西C# xamarin表单从按钮单击捕获列表视图,c#,xaml,listview,xamarin.forms,C#,Xaml,Listview,Xamarin.forms,下面的“我的代码”显示一个列表视图,当单击“请播放您的订单”按钮时,按下功能按钮(object sender,EventArgs e)。如何从ButtonPressed函数捕获整个listview 我试过类似的东西 void ButtonPressed(object sender, EventArgs e) { var listView = (ListView)sender;//but this did not work } <C
void ButtonPressed(object sender, EventArgs e)
{
var listView = (ListView)sender;//but this did not work
}
<ContentPage.Content>
<StackLayout>
<ListView ItemsSource="{Binding CompletedList}" HasUnevenRows="True" SeparatorVisibility="None">
<ListView.Header>
<Button Text="Please place your order" Clicked="ButtonPressed"/>
</ListView.Header>
<ListView.Footer>
<Label x:Name="Items" HorizontalTextAlignment="End" VerticalTextAlignment="Start" Margin="20,20" FontAttributes="Bold"/>
</ListView.Footer>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10" RowSpacing="10" ColumnSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding I_D}" VerticalOptions="End" IsVisible="False"/>
<Label Grid.Column="2" Grid.Row="1" Text="{Binding NameOfProduct}" VerticalOptions="End"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
void按钮按下(对象发送方,事件参数e)
{
var listView=(listView)sender;//但这不起作用
}
您的列表视图
绑定到CompletedList
,因此您只需要访问该变量,而不是列表视图本身 您的ListView绑定到CompletedList,因此您实际上只需要访问该变量,而不是ListView本身。如果您真的需要访问ListView本身,只需为它指定一个名称并以这种方式引用它。是的,但我在查找正确的语法时遇到了困难,例如,它是否为e.CompletedList?,它不起作用否,它没有作为参数传递给处理程序。这取决于它是在您的页面上定义的属性,还是在您的VM上定义的属性,等等。BindingContext是如何设置的?啊,是的,好的……我是从listView的BindingContext获得的。再次感谢杰森,如果你想要有标记的答案,写下来,我会打勾的。干杯