简体   繁体   English

Windows Phone更改列表框中textblock的文本

[英]Windows Phone change text of textblock in listbox

I have a listbox in my xaml page in my windows phone app. 我在Windows Phone应用程序的xaml页面中有一个列表框。

the itemsource of the listbox is set to data coming from the server. 列表框的itemsource设置为来自服务器的数据。

I need to set the text of a textblock/button inside this listbox according to data received from the server. 我需要根据从服务器接收到的数据来设置此列表框中的文本块/按钮的文本。

I cant bind the data directly, neither can I change the data coming from the server. 我无法直接绑定数据,也不能更改来自服务器的数据。

I need to do something like this:- 我需要做这样的事情:

if (Data from server == "Hey this is free")
    { Set textblock/button text to free }
else
    { Set textblock/button text to Not Free/Buy }

data from server (for this particular element) can have more than 2-3 types, for example it can be $5, $10, $15, Free or anything else 来自服务器的数据(针对该特定元素)可以具有2-3种以上的类型,例如,可以是$ 5,$ 10,$ 15,免费或其他任何类型

so only in case of free, I need to set text to free otherwise set it to Not Free/Buy. 因此,只有在免费的情况下,我才需要将文本设置为免费,否则将其设置为不免费/购买。

How can I access this textblock/button inside the listbox? 如何访问列表框中的此文本块/按钮?

You should use a Converter . 您应该使用Converter Here is how: 方法如下:

Start by declaring a class that implements IValueConverter . 首先声明一个实现IValueConverter的类。 This is where you will test the value received from the server and return the appropriate value. 在这里,您将测试从服务器收到的值并返回适当的值。

public sealed class PriceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value.ToString() == "Hey this is free")
        {
            return "free";
        }
        else
        {
            return "buy";
        }
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

At the top of your page, add the Namepace declaration: 在页面顶部,添加Namepace声明:

xmlns:local="clr-namespace:namespace-where-your-converter-is"

Declare the Converter: 声明转换器:

<phone:PhoneApplicationPage.Resources>
    <local:PriceConverter x:Key="PriceConverter"/>
</phone:PhoneApplicationPage.Resources>

And use it on the TextBlock: 并在TextBlock上使用它:

<TextBlock Text="{Binding Price,Converter={StaticResource PriceConverter}}"/>

You can define a value converter : 您可以定义一个值转换器

public class PriceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return String.Empty;
            var text = (string) value;
            return text.Contains("Free") ? "text to free" : text;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

and use in in your xaml 并在您的xaml中使用

<TextBlock Text="{Binding Text, Converter={StaticResource PriceConverter}}">

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

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