how do i escape a slash character in a WPF binding path, or how to work around?

末鹿安然 提交于 2019-12-19 15:44:29

问题


I'm just learning WPF, and I gragged a table from a datasource onto a window which generated XAML for each column.

Some of those columns had names that caused the following:

<DataGridTextColumn x:Name="_Rev_UnitColumn" Binding="{Binding Path=Rev/Unit}" Header="Rev/Unit" Width="SizeToHeader" />

This causes the column to come up blank (like me).


回答1:


I (kind of randomly) tried:

<DataGridTextColumn x:Name="_Rev_UnitColumn" Binding="{Binding Path=[Rev/Unit]}" Header="Rev/Unit" Width="SizeToHeader" />

And the result was everything worked as I expected it to. Looking at it again, I guess H.B.'s MSDN quote tells me this. When I read that (originally on MSDN before I even posted this question, then again here) I just didn't understand what "Inside indexers --comma-- the caret character (^) escapes the next character" meant.




回答2:


On MSDN there is an article on property paths which has a section on escape characters:

Inside indexers ([ ]), the caret character (^) escapes the next character.

You must escape (using XML entities) certain characters that are special to the XML language definition. Use & to escape the character "&". Use > to escape the end tag ">".

You must escape (using backslash \) characters that are special to the WPF XAML parser behavior for processing a markup extension.

  • Backslash (\) is the escape character itself.
  • The equal sign (=) separates property name from property value.
  • Comma (,) separates properties.
  • The right curly brace (}) is the end of a markup extension.

The slash is not listed here so i do not know if the backslash escape would work, but you can try.

(How exactly do you have a property name like that? It seems to be illegal both in XML and C#)



来源:https://stackoverflow.com/questions/6720285/how-do-i-escape-a-slash-character-in-a-wpf-binding-path-or-how-to-work-around

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!