在 Xamarin Forms 中编辑 ListView 所选项的 Label

我需要根据 ListView 的选定 Slider 的值来编辑 Label 值。当滑块的值大于 2 且小于 20 时,标签的文本应更改为“已编辑”我唯一拥有的是以下代码。可以用不同的方式来做吗?


public 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";

        }

    }

}

慕盖茨4494581
浏览 125回答 2
2回答

紫衣仙女

解决方案: 正如 Ivan 所说,您可以使用Converter请参考以下代码。public class ValueToTextConverter : IValueConverter{&nbsp; &nbsp; public object Convert(object value, Type targetType, object parameter, CultureInfo culture)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ((double)value < 20 && (double)value > 2)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Editado";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return "Ejemplo";&nbsp; &nbsp; }&nbsp; &nbsp; public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; }}当数据在OneWay或TwoWay绑定中从源移动到目标时,将调用 Convert 方法。value 参数是来自数据绑定源的对象或值。该方法必须返回数据绑定目标类型的值。在 xaml 中<ContentPage.Resources>&nbsp; &nbsp; <ResourceDictionary>&nbsp; &nbsp; &nbsp; &nbsp; <local:ValueToTextConverter x:Key="ValueToText" />&nbsp; &nbsp; </ResourceDictionary></ContentPage.Resources><StackLayout>&nbsp; &nbsp; <ListView x:Name="listaEjemplo" HasUnevenRows="True">&nbsp; &nbsp; &nbsp; &nbsp; <ListView.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ViewCell>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackLayout Orientation="Horizontal">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackLayout Orientation="Vertical">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Label Text="{Binding nombre}" Font="18"></Label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Slider x:Name="slider" Minimum="0" Maximum="20" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Label Text="{Binding Source={x:Reference slider},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path=Value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Converter={StaticResource ValueToText}}" TextColor="Gray"></Label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackLayout>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackLayout>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ViewCell>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </ListView.ItemTemplate>&nbsp; &nbsp; </ListView></StackLayout>https://i.stack.imgur.com/Tu5Jd.gif

江户川乱折腾

至少还有两种方法可以做到:Slider&nbsp;Value将与绑定TwoWay&nbsp;Mode。然后在值的设置器中更改绑定到标签的值(在该部分类似于您在上面的代码中所做的)绑定Label&nbsp;Value到Slider&nbsp;Value并声明Converter将转换Slider&nbsp;Value为所需Label&nbsp;Value.
打开App,查看更多内容
随时随地看视频慕课网APP