【问题标题】:How can control inside DataTemplate be found in Xamarin如何在 Xamarin 中找到 DataTemplate 内部的控件
【发布时间】:2021-10-18 13:46:46
【问题描述】:

我在 DataTemplate 中有一个控件。当该控件执行 Tapped 命令时,我希望它被隐藏而不必重新加载 ContentPage。

Page1.xaml

<StackLayout x:Name="sss" BindableLayout.ItemsSource="{Binding ....}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>      
            <Frame x:Name="follower" IsVisible="{Binding Follow}">
                <Label Text="SSSS1" TextColor="#fff" FontSize="11"></Label>
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer Tapped="follower_Tapped"/>
                </Frame.GestureRecognizers>
            </Frame>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

Page1.xaml.cs

FeedsViewModel feedsViewModel;
public Page1()
{
    BindingContext = feedsViewModel = new FeedsViewModel(Navigation);
    feedsViewModel.OnAppearing();
}

private void follower_Tapped(object sender, EventArgs e)
{
    string a = "s";
    //Find Control Frame x:Name="follower" to IsVisible False
    .......
}

follow_Tapped 时如何隐藏 Frame x:Name="follower" 控件。谁能帮我解决这个问题?谢谢

【问题讨论】:

  • 您不应该直接操作 DataTemplate 中项目的 UI - 您应该使用绑定来设置它们的属性
  • 能说的更具体更清楚点吗?谢谢
  • IsVisible 已经绑定到 Follow 属性,所以只需在模型中更改 Follow 的值,而不是尝试手动修改 UI 元素
  • 谢谢,我会再试一次

标签: xamarin


【解决方案1】:

您可以在当前 Frame 中设置IsVisible 属性。

 private void follower_Tapped(object sender, EventArgs e)
    {
        var frame = sender as Frame;
        frame.IsVisible = false;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    相关资源
    最近更新 更多