Is it possible to add more characters after a binding in xaml?

后端 未结 7 1051
小蘑菇
小蘑菇 2021-02-02 10:36

I was wondering something, and couldn\'t find any relevant topics. I have following binding :

Content=\"{x:Static resx:Resource.Form_OtherOption_Description}\"
<         


        
相关标签:
7条回答
  • 2021-02-02 11:06

    You can also use MultiBinding with StringFormat e.g:

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding StringFormat="ID {0} Name: {1} Age: {2}">
                <Binding Source="{x:Static resx:SomeResx.ID}"/>
                 <Binding Path="Name"/>
                 <Binding Path="Age"/>
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
    

    You can use this in a content control TextBlock TextBlock.Text (sorry I couldn't get the code to show up for this above)

    0 讨论(0)
  • 2021-02-02 11:11

    Try Binding's property StringFormat - it can do very simply what you want.

    0 讨论(0)
  • 2021-02-02 11:19

    You could accomplish this with something like:

    <TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description},
                             StringFormat={}{0}:}" />
    

    Edit: <Label>s Content property does not respect the StringFormat property of a binding apparently. Which I've found has been moved to the ContentStringFormat property on the <Label>.

    <Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"
           ContentStringFormat="{}{0}:" />
    
    0 讨论(0)
  • 2021-02-02 11:25

    Yes you can. Here I add "testing" after binding text(clouds.all) in windows phone.

    <TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
    
    0 讨论(0)
  • 2021-02-02 11:27

    if you use a label inside a progress bar you can use this way:

    <Label x:Name="Progress" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold" Foreground="White" Opacity=".7" 
           Content="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="{}{0}%">
    

    in this way you can visualize the value of progressbar with a % added.

    0 讨论(0)
  • 2021-02-02 11:27

    You can create a converter that takes the input string and adds the ":".

    public class AddStringToStringConverter : IValueConverter
    {
        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string input = value as string;
            string suffix = parameter as string;
    
            return input + suffix;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    }
    

    Xaml:

    <Window.Resources>
        <local:AddStringToStringConverter x:Key="AddStringToStringConverter"/>
    </Window.Resources>
    ...
    <Label Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description}, Converter={StaticResource AddStringToStringConverter}, ConverterParameter=:}"/>
    

    Or something like that. Tried it and it worked for my source at least.

    If you have whitespace and the like in you ConverterParameter you can use signle quotes to make sure it does not get disposed.

    Edit: Oh right... yeah... there's also StringFormat which i have never needed before, ehehehe...

    0 讨论(0)
提交回复
热议问题