Search code examples
c#wpfxamltextblock

WPF Insert variable in TextBlock


Is it possible to bind "AMOUNT" with it's value (i.e. update with corresponding global variable) using only XAML? If not, what i have write to replace AMOUNT with my var before showing the page?

https://i.sstatic.net/KvtfR.png

<TextBlock Height="231" Canvas.Left="120" TextWrapping="Wrap" Canvas.Top="459" Width="840" 
           FontFamily="Neo Sans Pro" FontSize="48" 
           Foreground="#FF006CB7" 
           VerticalAlignment="Top" HorizontalAlignment="Left" TextAlignment="Center">
  <Run Text="Для перечисления "/>
  <Run FontWeight="Bold" Text="AMOUNT"/>  
  <Run Text=" рублей в помощь детям с помощью банковской карты, пожалуйста, следуйте инструкции:"/>
</TextBlock>

Solution

  • What you need is a binding to a variable in your code-behind.

    Text="{Binding AMOUNT}"
    

    If this is - as you describe - a "global variable", you can bind like so:

    Text="{x:Static wpfApplication1:Globals.Amount}"
    

    The global variable definition could look like this:

    public class Globals
    {
        public static string Amount = "5000";
    }
    

    Note that the Text property of your text box requires a string.