WPF绑定格式的符号和

WPF绑定格式的符号和,wpf,data-binding,binding,wpf-4.0,Wpf,Data Binding,Binding,Wpf 4.0,我正在尝试创建一种字符串格式,该格式包含一个包含符号的文本字符串。我已经试过了;(没有空格),但两者都会导致VS2010设计器中出现异常。以下是我的工作内容: Text="{Binding Path=LearningOpportunities, StringFormat='Sharing & Learning Opportunitites:\{0\}'}" 如果我使用“&”,它告诉我“以符号“&”开头的实体引用或序列必须以分号“;”结尾”。如果我使用&;,它得到了一个Format

我正在尝试创建一种字符串格式,该格式包含一个包含符号的文本字符串。我已经试过了;(没有空格),但两者都会导致VS2010设计器中出现异常。以下是我的工作内容:

Text="{Binding Path=LearningOpportunities, StringFormat='Sharing & Learning Opportunitites:\{0\}'}"
如果我使用“&”,它告诉我“以符号“&”开头的实体引用或序列必须以分号“;”结尾”。如果我使用&;,它得到了一个FormatException:“索引(基于零)必须大于或等于零,并且小于参数列表的大小。”我已经有一段时间没有使用WPF进行web开发了,所以我觉得有点生疏。我怎样才能让它工作

编辑:有很多关于逃跑是罪魁祸首的猜测,从我所知道的,这都是不正确的。因此,为了解决这个问题,这里是什么起作用,什么不起作用,从等式中去掉符号和:

有效的格式:

StringFormat=Sharing and learning opportunitites:\{0\}
StringFormat='Sharing and learning opportunitites:\{0\}'
StringFormat={}Sharing and learning opportunitites:{0}
StringFormat='{}Sharing and learning opportunitites:{0}'
StringFormat=Sharing and learning opportunitites:{0}
StringFormat='Sharing and learning opportunitites:{0}'
StringFormat=Sharing and learning opportunitites:{}{0}
StringFormat=Sharing and learning opportunitites:{{0}}  (Outputs literal "{0}")
不起作用的格式:

StringFormat=Sharing and learning opportunitites:\{0\}
StringFormat='Sharing and learning opportunitites:\{0\}'
StringFormat={}Sharing and learning opportunitites:{0}
StringFormat='{}Sharing and learning opportunitites:{0}'
StringFormat=Sharing and learning opportunitites:{0}
StringFormat='Sharing and learning opportunitites:{0}'
StringFormat=Sharing and learning opportunitites:{}{0}
StringFormat=Sharing and learning opportunitites:{{0}}  (Outputs literal "{0}")
总之,您可以通过在字符串开头放置大括号{}来转义字符串中的所有大括号,也可以使用反斜杠转义单个大括号。令人惊讶的是,它仍然可以在没有大括号的情况下工作,但是在VisualStudio中语法着色非常难看。为了避免难看的语法着色,可以在格式字符串周围使用单引号


因此,对于转义,问题是在格式字符串中添加一个与符会导致异常,不管它是否转义。转义和未转义之间的唯一区别是它给出了不同的异常。

您需要使用
&,并删除充当转义字符的
\
。您还有额外的
字符,这些字符不是必需的。请尝试以下操作:

Text="{Binding Path=LearningOpportunities, StringFormat=Sharing & Learning Opportunitites:{0}}"
编辑编辑:

啊哈!原来是一个血腥的错误与苹果酒设计的表面

证明:

尝试以下XAML行序列:

<StackPanel>
    <!-- should show '$YoMamma' -->
    <TextBlock Text="{Binding Path=Value, StringFormat=&#x24;{0}}"/>
    <!-- should show '%YoMamma' -->
    <TextBlock Text="{Binding Path=Value, StringFormat=&#x25;{0}}"/>
    <!-- should show '&YoMamma', but crashes the designer -->
    <!--<TextBlock Text="{Binding Path=Value, StringFormat=&#x26;{0}}"/>-->
    <!-- should show '"YoMamma', but crashes the designer -->
    <!--<TextBlock Text="{Binding Path=Value, StringFormat=&#x27;{0}}"/>-->
    <!-- should show '(YoMamma' -->
    <TextBlock Text="{Binding Path=Value, StringFormat=&#x28;{0}}"/>
    <!-- should show ')YoMamma' -->
    <TextBlock Text="{Binding Path=Value, StringFormat=&#x29;{0}}"/>

</StackPanel>
应该有效,以及:

&#38;
编辑:

啊,是的-正如在评论中反复讨论的那样,问题不在于符号本身,而在于
绑定的周围大括号内替换标记的“逃逸”
-要解决这个问题,您实际上有三个选项:

编辑2:呸,我想markdown可能不喜欢我的帖子(但有一点我错了)——让我们看看我是否能拼凑出一个完整的例子:

以防万一,这里还有一个pastebin链接:

假设我们有这样一个上下文对象:

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 &amp; Bar:{0}}"/>

        <!-- should show '#2:Foo & Bar:YoMamma' -->
        <TextBlock>
            <TextBlock.Text>
                <Binding Path="Value" StringFormat="#2:Foo &amp; Bar:{0}"/>
            </TextBlock.Text>
        </TextBlock>

        <!-- will actually show the '{' and '}', so 'Foo & Bar:{0}' -->
        <TextBlock Text="{Binding Path=Value, StringFormat=Foo &amp; 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>
并添加以下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>

每当我想在绑定中使用
StringFormat
时,如果我的格式包含额外的文本或空格,我会在前面额外放一组花括号,如下所示:

StringFormat={}Sharing and learning opportunitites:{0}
我知道我在网上看到过使用其他方法的推荐信,但它们都曾让我失望过。我认为不同版本的WPF支持不同的
StringFormat
syntax,因此根据您使用的版本,其他语法也可以工作,但是我发现上面的语法是唯一一种相当通用的语法

此外,您还需要使用
&
以转义
&
字符

StringFormat={}Sharing &amp; learning opportunitites:{0}

您是否尝试过:嗯。在您尝试执行格式调用的位置添加代码-还有其他一些正在进行中。编辑以显示我已经尝试过转义符号@杰金鲍,这是上面的代码。啊,我明白了……试试这个?StringFormat=Foo&;棒:{}{0}有趣,就是这样。它的行为就像我第二次输入a'&'时会出现异常一样,但这是有效的。谢谢如果你把它作为答案,我会记下来。编辑把我的帖子弄乱了。我试着写我已经试过了。谢谢你有趣的回答。但是你试过了吗?第一个和第三个文本块中的符号(第一个字符为&;)导致了一个异常:“System.FormatException索引(基于零)必须大于或等于零,并且小于参数列表的大小。”@xr280xr-是的,所有这些都是我刚刚创建的一个wpf应用程序的直接结果;它显示的和预期的一样…你用这个代码得到了那个错误??是的,复制并粘贴到一个测试项目中。使用VS2010和.NET Framework4@xr280xr哈-找到了一个可能有用的东西…一个替代的符号和字符:
@xr280xr编辑-问题是没有使用
&