Advertisement
Guest User

WPF StringFormat

a guest
Mar 25th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace WpfApplication2
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         public MainWindow()
  24.         {
  25.             InitializeComponent();
  26.             this.DataContext = new Foo()
  27.                 {
  28.                     Value = "YoMamma",
  29.                     DateValue = DateTime.Now,
  30.                     NumberValue = 3.14m,
  31.                     AmpersandValue = "&"
  32.                 };
  33.         }
  34.     }
  35.  
  36.     public class Foo : INotifyPropertyChanged
  37.     {
  38.         private string _value;
  39.         public string Value
  40.         {
  41.             get { return _value; }
  42.             set { _value = value; FireNpc("Value"); }
  43.         }
  44.         private decimal _numberValue;
  45.         public decimal NumberValue
  46.         {
  47.             get { return _numberValue; }
  48.             set { _numberValue = value; FireNpc("NumberValue"); }
  49.         }
  50.         private DateTime _dateValue;
  51.         public DateTime DateValue
  52.         {
  53.             get { return _dateValue; }
  54.             set { _dateValue = value; FireNpc("DateValue"); }
  55.         }
  56.         // Heh...don't actually do this, naturally...
  57.         private string _ampersandValue;
  58.         public string AmpersandValue
  59.         {
  60.             get { return _ampersandValue; }
  61.             set { _ampersandValue = value; FireNpc("AmpersandValue"); }
  62.         }
  63.         public event PropertyChangedEventHandler PropertyChanged = delegate { };
  64.         private void FireNpc(string name)
  65.         {
  66.             PropertyChanged(this, new PropertyChangedEventArgs(name));
  67.         }
  68.     }
  69. }
  70.  
  71.  
  72. <Window x:Class="WpfApplication2.MainWindow"
  73.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  74.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  75.         Title="MainWindow" Height="350" Width="525">
  76.     <StackPanel>
  77.         <!-- should show '#1:Foo & Bar:YoMamma' -->
  78.         <TextBlock Text="{Binding Path=Value, StringFormat=#1:Foo &amp; Bar:{0}}"/>
  79.  
  80.         <!-- should show '#2:Foo & Bar:YoMamma' -->
  81.         <TextBlock>
  82.             <TextBlock.Text>
  83.                 <Binding Path="Value" StringFormat="#2:Foo &amp; Bar:{0}"/>
  84.             </TextBlock.Text>
  85.         </TextBlock>
  86.  
  87.         <!-- Alternate: should show '#1:Foo & Bar:YoMamma' -->
  88.         <TextBlock Text="{Binding Path=Value, StringFormat=#1:Foo &#38; Bar:{0}}"/>
  89.  
  90.         <!-- Alternate: should show '#2:Foo & Bar:YoMamma' -->
  91.         <TextBlock>
  92.             <TextBlock.Text>
  93.                 <Binding Path="Value" StringFormat="#2:Foo &#38; Bar:{0}"/>
  94.             </TextBlock.Text>
  95.         </TextBlock>
  96.        
  97.         <!-- will actually show the '{' and '}', so 'Foo & Bar:{0}' -->
  98.         <TextBlock Text="{Binding Path=Value, StringFormat=Foo &amp; Bar:{{0}}}"/>
  99.  
  100.         <!-- default 'custom' (there's a fun oxymoron) format - should be '$3.14' -->
  101.         <TextBlock Text="{Binding Path=NumberValue, StringFormat=c}"/>
  102.  
  103.         <!-- custom 'custom' (bear with me) format -->
  104.         <TextBlock Text="{Binding Path=DateValue, StringFormat=MM/dd/yyyy}"/>
  105.        
  106.         <!-- multi parameter formatting-->
  107.         <TextBlock>
  108.             <TextBlock.Text>
  109.                 <MultiBinding StringFormat="As of {2:MM/dd/yyyy}, {0} cost {1:c}">
  110.                     <Binding Path="Value"/>
  111.                     <Binding Path="NumberValue"/>
  112.                     <Binding Path="DateValue"/>
  113.                 </MultiBinding>
  114.             </TextBlock.Text>
  115.         </TextBlock>
  116.  
  117.         <!-- And a crazy-person's method, using multi parameter formatting-->
  118.         <TextBlock>
  119.             <TextBlock.Text>
  120.                 <MultiBinding StringFormat="{}{0} {1} {0} = {0}">
  121.                     <Binding Path="Value"/>
  122.                     <Binding Path="AmpersandValue"/>
  123.                 </MultiBinding>
  124.             </TextBlock.Text>
  125.         </TextBlock>
  126.  
  127.     </StackPanel>
  128. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement