亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何訪問代碼隱藏中的元素,它在 Xamarin.Foms 的 xaml 頁面中的

如何訪問代碼隱藏中的元素,它在 Xamarin.Foms 的 xaml 頁面中的

C#
慕村9548890 2022-11-21 21:48:04
我試圖通過單擊按鈕訪問在 DataTemplate 中聲明的條目,該條目實際上位于 ListView 的 ItemTemplate 中。<StackLayout>    <Button Text="GetEntryTemplate" Clicked="Button_Clicked"/>    <ListView x:Name="listView" ItemsSource="{Binding Customer}">        <ListView.ItemTemplate>            <DataTemplate>                <ViewCell>                    <Entry Text="Xamarin"/>                </ViewCell>            </DataTemplate>        </ListView.ItemTemplate>    </ListView></StackLayout>    private void Button_Clicked(object sender, EventArgs e)    {        var loadedTemplate = listView.ItemTemplate.CreateContent();        var view = ((loadedTemplate as ViewCell).View as Entry).Text;                }我試過 CreateContent(),它實際上并沒有顯示運行時的變化。有人可以幫我解決這個問題嗎?簡而言之,我需要通過單擊按鈕訪問現有條目實例(在 DataTemplate 中聲明)文本。
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您可以使用數據綁定來設置和獲取條目的文本。


在 xaml 中


<StackLayout>


    <Button Text="GetEntryTemplate" Clicked="Button_Clicked"/>


    <ListView x:Name="listView">


        <ListView.ItemTemplate>


            <DataTemplate>


                <ViewCell>


                    <Entry TextColor="Black" Text="{Binding Content,Mode=TwoWay}"/>


                </ViewCell>


            </DataTemplate>


        </ListView.ItemTemplate>


    </ListView>


</StackLayout>

在你的代碼后面


創建一個模式(例如我的模型稱為數據)


public class Data

{

    public string Content { get; set; }

}

并在 contentPage


public partial class MainPage : ContentPage

{


    public ObservableCollection<Data> MySource { get; set; }


    public MainPage()

    {

        InitializeComponent();


        BindingContext = this;




        MySource = new ObservableCollection<Data>()

        {

          new Data() {Content="Entry_1" },

        };


        listView.ItemsSource = MySource;


    }




    private void Button_Clicked(object sender, EventArgs e)

    {


        DisplayAlert("title", MySource[0].Content, "cancel");


    }

}

https://i.stack.imgur.com/DKzMR.gif

查看完整回答
反對 回復 2022-11-21
  • 1 回答
  • 0 關注
  • 95 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號