编辑编辑:
啊哈!原来是苹果酒设计表面的血腥错误!
证明:
试试这个 XAML 行序列:
<StackPanel>
<!-- should show '$YoMamma' -->
<TextBlock Text="{Binding Path=Value, StringFormat=${0}}"/>
<!-- should show '%YoMamma' -->
<TextBlock Text="{Binding Path=Value, StringFormat=%{0}}"/>
<!-- should show '&YoMamma', but crashes the designer -->
<!--<TextBlock Text="{Binding Path=Value, StringFormat=&{0}}"/>-->
<!-- should show '"YoMamma', but crashes the designer -->
<!--<TextBlock Text="{Binding Path=Value, StringFormat='{0}}"/>-->
<!-- should show '(YoMamma' -->
<TextBlock Text="{Binding Path=Value, StringFormat=({0}}"/>
<!-- should show ')YoMamma' -->
<TextBlock Text="{Binding Path=Value, StringFormat=){0}}"/>
</StackPanel>
我提交了一个错误报告来连接,我们看看是否有人回应:
https ://connect.microsoft.com/VisualStudio/feedback/details/782059/cider-vs2010-designer-bug-with-binding-using-escaped-字符串格式的实体
这个答案的其余部分是半没有意义的,尽管可能有用,因为“错误”在设计者身上。
这里要记住的是 XAML是XML,因此您需要相应地对 & 符号进行编码:
&
应该工作,以及:
&
编辑:
啊,是的 - 正如评论中反复讨论的那样,问题不在于&符号本身,而在于 a 周围大括号内替换标记的“转义” Binding
- 要解决这个问题,你实际上有三个选择:
编辑 2:呸,我认为降价可能不喜欢我的帖子(而且我在某一点上错了) - 让我们看看我是否可以拼凑一个完整的例子:
以防万一,这里还有一个 pastebin 链接:http:
//pastebin.com/yfrpvxs1
假设我们有一个这样的上下文对象:
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));
}
}
还有一个窗口代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new Foo()
{
Value = "YoMamma",
DateValue = DateTime.Now,
NumberValue = 3.14m
};
}
}
让我们 XAML!
<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>
<!-- 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>
</StackPanel>
</Window>
现在,借助它MultiBinding
,我们可以做一些愚蠢的事情——让我们将它添加到我们的上下文中:
// 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 = "&"
};
并添加此 XAML:
<!-- 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>