简体   繁体   中英

WPF Insert variable in TextBlock

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

http://i.imgur.com/SDrV0rs.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>

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.

Using MVVM; in very broad strokes :

  1. Create a class with a string property: eg

    public class MyViewModel { public string Amount { get { return "..."; } } }

  2. Assign an instance of the class above to the DataContext of the view.

    var viewModel = new MyViewModel(); view.DataContext = viewModel;

  3. Using a binding expression in the XAML

    ...TextBlock Text="{Binding Amount}"... />

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