9

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).

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121

3 Answers3

6

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#)

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
6

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.

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • I recently came across a similar problem. Just for completion's sake, the thing about comma and caret is that you can escape the comma if your column has it in its name, like so: `Binding={Binding Path=[Hello^, World]}`. – Arturo Torres Sánchez Mar 28 '19 at 14:56
0

If you need to mask a path with : character, you can try using parenthesis char (path)

Example usage

<Style x:Key="DefaultButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="materialDesign:ButtonAssist.CornerRadius" Value="24"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border CornerRadius="{Binding (materialDesign:ButtonAssist.CornerRadius), RelativeSource={RelativeSource AncestorType=Button}}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Aleksey Pavlov
  • 148
  • 2
  • 11