在 ConverterParameter 中使用反斜杠
我遇到以下问题。
我想将字符串定界为某个符号。这可以是逗号、空格、制表符或其他内容。 我将该分隔符存储在我的依赖属性 Delimiter
中。
在我的 WPF 代码中,我有以下单选按钮,应检查 Delimiter
是否属于该单选按钮。 这是 WPF 代码:
<RadioButton IsChecked="{Binding ElementName=view, Path=Delimiter, Converter={StaticResource MyConverterToCheckForEquality}, ConverterParameter=\t}" Tag="\t" />
如果我检查转换器中的参数,则值为“t”。
我想要 '\t' 所以我尝试了以下选项:
- \t
- \\t
- \\\t
- '\t'
- '\\t'
- '\\\t'
- & #92;t
- '& #92;t'
- {}{\t}
- '{}{\t}'
(注意:我添加了额外的空格和反斜杠以在此处正确显示它)。
结果非常令人悲伤...三重和双反斜杠给了我双反斜杠,单反斜杠给了我一个没有。 & #92;也给了我零反斜杠。
也许最令人沮丧的事情是,当我使用 Tag 属性设置分隔符 \t 时效果很好,并且在此过程中没有额外转义...
有人可以解释为什么会发生这种情况,为什么这些选项都不起作用以及如何修复它?
更新
当我在多重数据触发器中使用多重绑定时,结果是不同的:
<Binding ElementName="view" Path="Delimiter" Converter="{StaticResource MyConverterToCheckForEquality}" ConverterParameter="\\\t" />
转换器中的参数值现在是“\\\\\\t”,但是我仍然无法使其工作。
我可以替换该值,但这意味着我必须在代码隐藏文件中使用不同的变量,这将非常难看。我想虽然没有其他解决方案。
I have the following problem.
I want to delimit a string be a certain symbol. This could be a comma, a space, a tab or something else.
I store that delimiter symbol in my dependency property Delimiter
.
In my WPF code I have the following radio button which should be checked if Delimiter
belongs to that radio button.
This is the WPF code:
<RadioButton IsChecked="{Binding ElementName=view, Path=Delimiter, Converter={StaticResource MyConverterToCheckForEquality}, ConverterParameter=\t}" Tag="\t" />
If I check out the parameter in my converter the value is 't'.
I wanted '\t' so I tried the following options:
- \t
- \\t
- \\\t
- '\t'
- '\\t'
- '\\\t'
- & #92;t
- '& #92;t'
- {}{\t}
- '{}{\t}'
(Note: I added extra spaces and backslashes to display it here correctly).
The result is very saddening... Triple and Double backslash gives me double backslash, Single gives me none. & #92; also gives me zero backslashes.
Maybe the most frustrating thing is that when I use the Tag property to set the delimiter \t just works fine and is not extra escaped in the process...
Can somebody explain why this happens, why none of these options worked and how to fix it?
UPDATE
When I use a multibinding within a multidatatrigger the result is diffrent:
<Binding ElementName="view" Path="Delimiter" Converter="{StaticResource MyConverterToCheckForEquality}" ConverterParameter="\\\t" />
The parameter value in my converter is now '\\\\\\t', however I still can't make it work.
I can replace the value but that would mean I have to use a different variable in my code behind file, which would be pretty ugly. I guess there is no other solution though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
代表
\t
(#9 是水平制表符的 ASCII 等效项)我通过将它作为转换器参数传递到我的转换器中来测试它,并且它显示正确。这是我的测试代码:
XAML:
C# codebehind:
当我运行此代码时,窗口显示一个文本框,其中包含文本“成功!”
Try this:
represents
\t
(#9 is the ASCII equivalent of horizontal tab)I tested it by passing it into my converter as the converter parameter, and it shows up correctly. Here's my test code:
XAML:
C# codebehind:
When I run this, the window shows a TextBox with the text "Success!"
您可能只需要
.Replace("\\", "\")
斜杠......MSDN 说以下内容,但显然这是不正确的
You might just have to
.Replace("\\", "\")
the slashes....MSDN says the following, but apparently it's incorrect
为什么不尝试这样的事情:
那么
Why not trying something like that :
Then