Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WpfApplication2
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- this.DataContext = new Foo()
- {
- Value = "YoMamma",
- DateValue = DateTime.Now,
- NumberValue = 3.14m,
- AmpersandValue = "&"
- };
- }
- }
- public class Foo : INotifyPropertyChanged
- {
- private string _value;
- public string Value
- {
- get { return _value; }
- set { _value = value; FireNpc("Value"); }
- }
- private decimal _numberValue;
- public decimal NumberValue
- {
- get { return _numberValue; }
- set { _numberValue = value; FireNpc("NumberValue"); }
- }
- private DateTime _dateValue;
- public DateTime DateValue
- {
- get { return _dateValue; }
- set { _dateValue = value; FireNpc("DateValue"); }
- }
- // Heh...don't actually do this, naturally...
- private string _ampersandValue;
- public string AmpersandValue
- {
- get { return _ampersandValue; }
- set { _ampersandValue = value; FireNpc("AmpersandValue"); }
- }
- public event PropertyChangedEventHandler PropertyChanged = delegate { };
- private void FireNpc(string name)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(name));
- }
- }
- }
- <Window x:Class="WpfApplication2.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
- <StackPanel>
- <!-- should show '#1:Foo & Bar:YoMamma' -->
- <TextBlock Text="{Binding Path=Value, StringFormat=#1:Foo & Bar:{0}}"/>
- <!-- should show '#2:Foo & Bar:YoMamma' -->
- <TextBlock>
- <TextBlock.Text>
- <Binding Path="Value" StringFormat="#2:Foo & Bar:{0}"/>
- </TextBlock.Text>
- </TextBlock>
- <!-- Alternate: should show '#1:Foo & Bar:YoMamma' -->
- <TextBlock Text="{Binding Path=Value, StringFormat=#1:Foo & Bar:{0}}"/>
- <!-- Alternate: should show '#2:Foo & Bar:YoMamma' -->
- <TextBlock>
- <TextBlock.Text>
- <Binding Path="Value" StringFormat="#2:Foo & Bar:{0}"/>
- </TextBlock.Text>
- </TextBlock>
- <!-- will actually show the '{' and '}', so 'Foo & Bar:{0}' -->
- <TextBlock Text="{Binding Path=Value, StringFormat=Foo & Bar:{{0}}}"/>
- <!-- default 'custom' (there's a fun oxymoron) format - should be '$3.14' -->
- <TextBlock Text="{Binding Path=NumberValue, StringFormat=c}"/>
- <!-- custom 'custom' (bear with me) format -->
- <TextBlock Text="{Binding Path=DateValue, StringFormat=MM/dd/yyyy}"/>
- <!-- multi parameter formatting-->
- <TextBlock>
- <TextBlock.Text>
- <MultiBinding StringFormat="As of {2:MM/dd/yyyy}, {0} cost {1:c}">
- <Binding Path="Value"/>
- <Binding Path="NumberValue"/>
- <Binding Path="DateValue"/>
- </MultiBinding>
- </TextBlock.Text>
- </TextBlock>
- <!-- And a crazy-person's method, using multi parameter formatting-->
- <TextBlock>
- <TextBlock.Text>
- <MultiBinding StringFormat="{}{0} {1} {0} = {0}">
- <Binding Path="Value"/>
- <Binding Path="AmpersandValue"/>
- </MultiBinding>
- </TextBlock.Text>
- </TextBlock>
- </StackPanel>
- </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement