简体   繁体   English

如何在 xamarin.forms 中获取 ListView 的子项?

[英]How to get children of the ListView in xamarin.forms?

<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>

As you see in code ListView having main two child named "stkNewsLetter" and "stkNewLetterDetail".正如您在代码 ListView 中看到的,主要的两个孩子分别名为“stkNewsLetter”和“stkNewLetterDetail”。 First Stack having Image and I want to change visibility of second stack on Image tap event.第一个堆栈具有图像,我想在图像点击事件中更改第二个堆栈的可见性。

In windows phone It will possible to get UI elements of List view but In xamarin.forms how it is possible?在 Windows Phone 中可以获得列表视图的 UI 元素,但在 xamarin.forms 中怎么可能?

You should bind to the Visability property on the stackLayout in the same way you are binding the Text.您应该以与绑定 Text 相同的方式绑定到 stackLayout 上的 Visability 属性。 If the stackLayout doesn't have a visibility property you may need to wrap it within another control that does.如果 stackLayout 没有可见性属性,您可能需要将其包装在另一个具有可见性的控件中。

The click should change a visibility property in you viewmodel which will get picked up by the binding mentioned above.单击应更改视图模型中的可见性属性,该属性将由上述绑定获取。

For those who came here to get the child elements through Name like me,here is the solution:对于那些像我一样通过 Name 来获取子元素的人,这里是解决方案:

var selectedItem = //any item selected within listview through OnItemTapped
StackLayout child1 = selectedItem.FindByName<StackLayout>("stkNewsLetter");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM