Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDITEDITEDIT:</p> <p>Aha! Turns out to be a bloody bug with the Cider design surface!</p> <p>Proof:</p> <p>Try this sequence of XAML lines:</p> <pre><code>&lt;StackPanel&gt; &lt;!-- should show '$YoMamma' --&gt; &lt;TextBlock Text="{Binding Path=Value, StringFormat=&amp;#x24;{0}}"/&gt; &lt;!-- should show '%YoMamma' --&gt; &lt;TextBlock Text="{Binding Path=Value, StringFormat=&amp;#x25;{0}}"/&gt; &lt;!-- should show '&amp;YoMamma', but crashes the designer --&gt; &lt;!--&lt;TextBlock Text="{Binding Path=Value, StringFormat=&amp;#x26;{0}}"/&gt;--&gt; &lt;!-- should show '"YoMamma', but crashes the designer --&gt; &lt;!--&lt;TextBlock Text="{Binding Path=Value, StringFormat=&amp;#x27;{0}}"/&gt;--&gt; &lt;!-- should show '(YoMamma' --&gt; &lt;TextBlock Text="{Binding Path=Value, StringFormat=&amp;#x28;{0}}"/&gt; &lt;!-- should show ')YoMamma' --&gt; &lt;TextBlock Text="{Binding Path=Value, StringFormat=&amp;#x29;{0}}"/&gt; &lt;/StackPanel&gt; </code></pre> <p>I submitted a bug report to connect, we'll see if anyone responds: <a href="https://connect.microsoft.com/VisualStudio/feedback/details/782059/cider-vs2010-designer-bug-with-binding-using-escaped-entities-in-stringformat" rel="nofollow">https://connect.microsoft.com/VisualStudio/feedback/details/782059/cider-vs2010-designer-bug-with-binding-using-escaped-entities-in-stringformat</a></p> <p>Rest of this answer is semi-moot, albeit potentially useful, since the "bug" is on the designer.</p> <p>The thing to remember here is that XAML <strong>is</strong> XML, so you need to encode ampersands accordingly:</p> <pre><code>&amp;amp; </code></pre> <p>should work, as well as:</p> <pre><code>&amp;#38; </code></pre> <p>EDIT:</p> <p>Ah, yes - so as discussed in the back-and-forth in the comments, the problem lay not with the ampersand <em>per se</em>, but the "escaping" of the replacement markers within the surrounding braces of a <code>Binding</code> - to fix this, you've actually got three options:</p> <p>EDIT 2: Bah, I think markdown might not like my post (and I was wrong on one point anyway) - let's see if I can cobble a full example together:</p> <p>Just in case, here's a pastebin link as well: <a href="http://pastebin.com/yfrpvxs1" rel="nofollow">http://pastebin.com/yfrpvxs1</a></p> <p>So say we've got a context object like so:</p> <pre><code>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"); } } public event PropertyChangedEventHandler PropertyChanged = delegate { }; private void FireNpc(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } </code></pre> <p>And a window codebehind:</p> <pre><code>public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new Foo() { Value = "YoMamma", DateValue = DateTime.Now, NumberValue = 3.14m }; } } </code></pre> <p>Let's XAML!</p> <pre><code>&lt;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"&gt; &lt;StackPanel&gt; &lt;!-- should show '#1:Foo &amp; Bar:YoMamma' --&gt; &lt;TextBlock Text="{Binding Path=Value, StringFormat=#1:Foo &amp;amp; Bar:{0}}"/&gt; &lt;!-- should show '#2:Foo &amp; Bar:YoMamma' --&gt; &lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;Binding Path="Value" StringFormat="#2:Foo &amp;amp; Bar:{0}"/&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;!-- will actually show the '{' and '}', so 'Foo &amp; Bar:{0}' --&gt; &lt;TextBlock Text="{Binding Path=Value, StringFormat=Foo &amp;amp; Bar:{{0}}}"/&gt; &lt;!-- default 'custom' (there's a fun oxymoron) format - should be '$3.14' --&gt; &lt;TextBlock Text="{Binding Path=NumberValue, StringFormat=c}"/&gt; &lt;!-- custom 'custom' (bear with me) format --&gt; &lt;TextBlock Text="{Binding Path=DateValue, StringFormat=MM/dd/yyyy}"/&gt; &lt;!-- multi parameter formatting--&gt; &lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding StringFormat="As of {2:MM/dd/yyyy}, {0} cost {1:c}"&gt; &lt;Binding Path="Value"/&gt; &lt;Binding Path="NumberValue"/&gt; &lt;Binding Path="DateValue"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>Now, piggybacking on that <code>MultiBinding</code>, we can do something stupid - let's add this to our context:</p> <pre><code> // Heh...don't actually do this, naturally... private string _ampersandValue; public string AmpersandValue { get { return _ampersandValue; } set { _ampersandValue = value; FireNpc("AmpersandValue"); } } this.DataContext = new Foo() { Value = "YoMamma", DateValue = DateTime.Now, NumberValue = 3.14m, AmpersandValue = "&amp;" }; </code></pre> <p>And add this XAML:</p> <pre><code> &lt;!-- And a crazy-person's method, using multi parameter formatting--&gt; &lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding StringFormat="{}{0} {1} {0} = {0}"&gt; &lt;Binding Path="Value"/&gt; &lt;Binding Path="AmpersandValue"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload