简体   繁体   English

绑定后能否将文本添加到文本块?

[英]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?所以我有一个使用 (Text="x") 绑定属性的 Textblock,但是有没有办法向它添加更多文本?

So this is the line in XAML所以这是 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".它只显示 Stock atm 的数量,但我希望它显示“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一般来说,我们经常添加带有静态文本X 的新 TextBlock 并放置在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.另一种方法是使用IValueConverterX附加到Stock属性。

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>

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

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