简体   繁体   中英

Able to add Text to a textblock after binding?

so i've a Textblock that binds a property using the (Text="x"), but is there a way to add more text to it?

So this is the line in XAML

 <TextBlock Name="UpdatedStock" FontSize="12" Text="{x:Bind Stock, Mode=TwoWay}"  HorizontalAlignment="Left" />

and it only says the number of Stock atm, but i want it to say "X in stock". But i cant add more text to it, is there a way somehow to add it on the same line or i have to do it somewhere else?

Kinds regards

Able to add Text to a textblock after binding?

You have many ways to approach. In general we often add new TextBlock with static text X and place left of UpdatedStock

<RelativePanel>
    <TextBlock
        x:Name="UpdatedStock"
        FontSize="12"
        RelativePanel.AlignLeftWithPanel="True"
        Text="{x:Bind Stock, Mode=OneWay}" />
    <TextBlock Margin="2,0,0,0"
        FontSize="12"
        RelativePanel.RightOf="UpdatedStock"
        Text="X" />
</RelativePanel>

And the other way is use IValueConverter to append X to Stock propety.

public class StringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
            return null;

        return value.ToString()+ "X";
    }

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

Usage

<Page.Resources>
    <local:StringConverter x:Key="StrConverter" />
</Page.Resources>
<Grid>

    <TextBlock
        x:Name="UpdatedStock"
        FontSize="12"
        RelativePanel.AlignLeftWithPanel="True"
        Text="{x:Bind Stock, Mode=OneWay, Converter={StaticResource StrConverter}}" />

</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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