C# 如何在xamarin.forms中获取ListView的子级?
正如您在代码列表视图中看到的,主要有两个子元素,分别名为“stkNewsLetter”和“stkNewLetterDetail”。第一个堆栈有图像,我想在图像点击事件中更改第二个堆栈的可见性C# 如何在xamarin.forms中获取ListView的子级?,c#,xamarin,xamarin.ios,xamarin.android,xamarin.forms,C#,Xamarin,Xamarin.ios,Xamarin.android,Xamarin.forms,正如您在代码列表视图中看到的,主要有两个子元素,分别名为“stkNewsLetter”和“stkNewLetterDetail”。第一个堆栈有图像,我想在图像点击事件中更改第二个堆栈的可见性 在windows phone中,可以获取列表视图的UI元素,但在xamarin.forms中可以获取哪些元素?您应该以绑定文本的相同方式绑定到stackLayout上的Visibility属性。如果stackLayout没有可见性属性,则可能需要将其包装到另一个具有可见性属性的控件中 单击应该会更改view
在windows phone中,可以获取列表视图的UI元素,但在xamarin.forms中可以获取哪些元素?您应该以绑定文本的相同方式绑定到stackLayout上的Visibility属性。如果stackLayout没有可见性属性,则可能需要将其包装到另一个具有可见性属性的控件中
单击应该会更改viewmodel中的可见性属性,该属性将通过上述绑定获取。对于像我这样通过名称获取子元素的人,以下是解决方案:
<ListView x:Name="ListNewsLetter" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" SeparatorVisibility="Default" VerticalOptions="Fill" ItemTapped="ListNewsLetter_ItemTapped" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout HorizontalOptions="Fill" VerticalOptions="Center" Padding="5">
<StackLayout Padding="1" BackgroundColor="#f15a23" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout x:Name="stkNewsLetter" VerticalOptions="Center" BackgroundColor="#f15a23" Orientation="Horizontal" HeightRequest="30" Padding="6,0,6,0">
<Label x:Name="lblName" Text="{Binding NewsHeadline}" HorizontalOptions="StartAndExpand" TextColor="White" VerticalTextAlignment="Center" FontSize="Small"/>
<Image x:Name="imgPlus_Minus" HeightRequest="20" WidthRequest="20" Source="ico_down_arrow2_right.png" HorizontalOptions="End" ClassId="{Binding TapId}">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="img_Tapped" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
<StackLayout x:Name="stkNewsLetterDetail" Orientation="Vertical" BackgroundColor="#ffffff" IsVisible="true" HorizontalOptions="FillAndExpand" Padding="6,0,6,0">
<Label x:Name="lblDate" Text="{Binding NewsDate}" HorizontalOptions="StartAndExpand" VerticalTextAlignment="Start" TextColor="#585858" FontSize="Small"/>
<Label x:Name="lblDetail" Text="{Binding NewsDetails}" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Start" TextColor="#585858" FontSize="Small"/>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
var selectedItem=//通过OnItemMapped在listview中选择的任何项目
StackLayout child1=selectedItem.FindByName(“stkNewsLetter”);
我没有得到这样的listview嵌套项。好吧,我错了。您需要编写选定的项目,而不是ListNewsLetter
var selectedItem = //any item selected within listview through OnItemTapped
StackLayout child1 = selectedItem.FindByName<StackLayout>("stkNewsLetter");