3

I'm binding the Expander.Header property in XAML to a backing public property of string type in my ViewModel using the MVVM pattern:

public string EnumName {...}

I'm setting this property to "X_Y_Z" but for some strange reason the Expander Header is removing the first underscore character and it is displayed as XY_Z. I tried adding a "\" before or setting it to @"X_Y_Z" but no luck.

Here my XAML binding portion:

<Expander IsExpanded="true" Header="{Binding EnumName}">

Anybody know why I'm getting this behavior? Is it taking the first "_" as a keyboard shortcut or a meta-character?

Thanks,

Adolfo Perez
  • 2,834
  • 4
  • 41
  • 61
  • Please post answers as actual answers, they do not belong in the question. Also that wrapping is a good idea, i thought of that as well but failed to mention it. By the way, shouldn't the current DataContext be correct already? – H.B. Aug 28 '12 at 17:05
  • No, the DataContext is not correct as it is, I didn't mention that the Expander is inside a DataTemplate used in a parent ListView/GridView/GridViewColumn/@CellTemplate – Adolfo Perez Aug 28 '12 at 17:09
  • I posted my solution as an answer but cannot mark it as accepted until 2 days. Thanks again for your help – Adolfo Perez Aug 28 '12 at 17:10
  • 1
    You're welcome, you are not required to accept you own solution by the way, it's just important that question and answers are separated. Also in cases like this where one answer leads to another it would also be possible to just edit that into the existing answer. – H.B. Aug 28 '12 at 17:14

3 Answers3

4

Probably converted to an access-key because there is a Label or a ContentPresenter with RecognizesAccessKey set to true in your Expander.Template.

You can for example switch out the template or escape underscores in your values (two underscores in a row).

H.B.
  • 166,899
  • 29
  • 327
  • 400
3
<Expander Header="{Binding Path=Name}">                                    
   <Expander.HeaderTemplate>
      <DataTemplate>
          <TextBlock Text="{Binding}"/>
      </DataTemplate>
   </Expander.HeaderTemplate>
</Expander>
Buzzy
  • 1,783
  • 18
  • 15
1

@H.B. pointed me to the right direction. I ended up setting a Header Template for my Expander as follows, accessing the DataContext of the ancestor ListViewItem:

<Expander.HeaderTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Path=DataContext.EnumName,RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
    </DataTemplate>
</Expander.HeaderTemplate>

Since I'm now using a TextBlock it does not remove any of the "_" characters in my string.

Adolfo Perez
  • 2,834
  • 4
  • 41
  • 61