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

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

在 Xamarin Forms 中編輯 ListView 所選項的 Label

在 Xamarin Forms 中編輯 ListView 所選項的 Label

C#
慕蓋茨4494581 2022-12-04 11:13:16
我需要根據 ListView 的選定 Slider 的值來編輯 Label 值。當滑塊的值大于 2 且小于 20 時,標簽的文本應更改為“已編輯”我唯一擁有的是以下代碼??梢杂貌煌姆绞絹碜鰡幔縫ublic partial class MainPage : ContentPage{    readonly List<Tarea> listaTarea = new List<Tarea>();    public MainPage()    {        InitializeComponent();        llenarLista();        listaEjemplo.ItemsSource = listaTarea;    }    public void llenarLista()    {        listaTarea.Add(new Tarea{            nombre = "Alex1",            valor="10",            descripcion = "Ejemplo"        });        listaTarea.Add(new Tarea        {            nombre = "Alex2",            valor = "20",            descripcion = "Ejemplo"        });        listaTarea.Add(new Tarea        {            nombre = "Alex3",            valor = "30",            descripcion = "Ejemplo"        });        listaTarea.Add(new Tarea        {            nombre = "Alex4",            valor = "40",            descripcion = "Ejemplo"        });        listaTarea.Add(new Tarea        {            nombre = "Alex5",            valor = "50",            descripcion = "Ejemplo"        });        /*        if(listaTarea[2].valor.Equals("30"))        {            listaTarea[2].descripcion = "Cambiado";        }*/    }    void Handle_ValueChanged(object sender, Xamarin.Forms.ValueChangedEventArgs e)    {        var sliders = sender as Slider;        var item = sliders.Parent.BindingContext as Tarea;        double valor = sliders.Value;        if(valor > 2 && valor<20)        {            item.nombre = "Editado";        }    }}
查看完整描述

2 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

解決方案: 正如 Ivan 所說,您可以使用Converter


請參考以下代碼。


public class ValueToTextConverter : IValueConverter

{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

        if ((double)value < 20 && (double)value > 2)

        {

            return "Editado";

        }


        return "Ejemplo";

    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

    {

        throw new NotImplementedException();

    }

}

當數據在OneWay或TwoWay綁定中從源移動到目標時,將調用 Convert 方法。value 參數是來自數據綁定源的對象或值。該方法必須返回數據綁定目標類型的值。


在 xaml 中


<ContentPage.Resources>

    <ResourceDictionary>

        <local:ValueToTextConverter x:Key="ValueToText" />

    </ResourceDictionary>

</ContentPage.Resources>



<StackLayout>

    <ListView x:Name="listaEjemplo" HasUnevenRows="True">

        <ListView.ItemTemplate>

            <DataTemplate>

                <ViewCell>

                    <StackLayout Orientation="Horizontal">

                        <StackLayout Orientation="Vertical">

                            <Label Text="{Binding nombre}" Font="18"></Label>

                            <Slider x:Name="slider" Minimum="0" Maximum="20" />

                            <Label Text="{Binding Source={x:Reference slider},

                                Path=Value,

                                Converter={StaticResource ValueToText}}" TextColor="Gray"></Label>

                        </StackLayout>

                    </StackLayout>

                </ViewCell>

            </DataTemplate>

        </ListView.ItemTemplate>

    </ListView>

</StackLayout>

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

查看完整回答
反對 回復 2022-12-04
?
江戶川亂折騰

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

至少還有兩種方法可以做到:

  • Slider Value將與綁定TwoWay Mode。然后在值的設置器中更改綁定到標簽的值(在該部分類似于您在上面的代碼中所做的)

  • 綁定Label ValueSlider Value并聲明Converter將轉換Slider Value為所需Label Value.


查看完整回答
反對 回復 2022-12-04
  • 2 回答
  • 0 關注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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