简体   繁体   中英

Xamarin Binding and normal text in a label

is there a posibility to have a label, with a "standart" text AND a binding content? this is what i´m looking for:

 <Label Text="Hello, this is {Binding name}"/> 

but this doesnt work. i know, i could make it like this:

  <Label Text="Hello, this is "/>
  <Label Text="{Binding name}"/>

but i would really like to do it in only one label, because if there's a wordwrap, it doesnt look that well.

thanks a lot

You can achieve this using FormattedText property on Label

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="Hello, this is ">
            <Span Text="{Binding name}">
        </FormattedString>
    </Label.FormattedText>
</Label>

You should do it in code, so something like this.

private string _name;
public string Name
{
get
{
return String.Format("Hello, this is {0}", _name);
}
set
{
_name = value;
RaisePropertyChanged("Name"); //bear in mind this is depended on MVVM framework you are using
}
}

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