13

The following Microsoft example code contains the following:

<Grid>
...     
  <Border Name="Content" ... >
...     
  </Border>
</Grid>
<ControlTemplate.Triggers>
  <Trigger Property="IsExpanded" Value="True">
     <Setter TargetName="ContentRow" Property="Height"
             Value="{Binding ElementName=Content,Path=DesiredHeight}" />
  </Trigger>
...
</ControlTemplate.Triggers>

When run, however, this code generates the following databinding error:

System.Windows.Data Error: 39 : BindingExpression path error: 'DesiredHeight' property not found on 'object' ''Border' (Name='Content')'. BindingExpression:Path=DesiredHeight; DataItem='Border' (Name='Content'); target element is 'RowDefinition' (HashCode=2034711); target property is 'Height' (type 'GridLength')

Despite this error, the code works correctly. I have looked through the documentation and DesiredHeight does not appear to be a member of Border. Can anyone explain where DesiredHeight is coming from? Also, is there any way to resolve/suppress this error so my program output is clean?

7 Answers 7

9

You can see that property in the code part of your application

Edit:

Border content = new Border();
int desiredHeight = content.DesiredSize.Height;
int desiredWidth = content.DesiredSize.Width;

To solve the problem try binding it to the Height attribute, since DesiredHeight doesn't seem to be available in the XAML markup of the Border control.

2
  • Okay, that works (though DesiredSize.Height is a double). So does DesiredHeight just map to Border.DesiredSize.Height? Is this some sort of undocumented property? May 19, 2009 at 17:13
  • I'm sure it's not undocumented, the thing is that XAML controls size themselves based on their content, even if you set the height to 100, it could change for any other reason, and the new height and width will be contained in the ActualHeight and ActualWidth properties respectively. If you want your controls to have a fixed size you should set the MinWidth and MinHeight instead, but most of the time I don't recommend this. Mainly all of this is to solve resizing problems that we had with WindowsApplications.
    – Carlo
    May 19, 2009 at 17:20
6

Had the same issue. Was using a custom Expander in a custom ComboBox. None of the above worked for me, binding to Height broke the functionality of the Expander, using a StackPanel also broke the displaying of the items in each group. I found:

<Setter TargetName="ContentRow" Property="Height" Value="Auto"/>
1
  • This works for me, and is probably the best answer, since using Auto effectively will use the DesiredHeight, but with the most concise binding. Aug 3, 2014 at 15:28
4

I've ran into this. Along the lines of what user275587 said, their example works because the trigger removes the Heigth="0" on the RowDefination.

So I switch the height setting/trigger logic , so the RowDefination has no Height set

<Grid.RowDefinitions>
     <RowDefinition Height="Auto"/>
     <RowDefinition Name="ContentRow" />
</Grid.RowDefinitions>
...
<ControlTemplate.Triggers>
     <Trigger Property="IsExpanded" Value="False">
             <Setter TargetName="ContentRow" Property="Height" Value="0" />
     </Trigger>
</ControlTemplate.Triggers>
3

Try it.

<Setter
    TargetName="content"
    Property="Height"
    Value="{Binding ElementName=content, Path=DesiredHeight}"
/>
    ↓
<Setter TargetName="content" Property="Height" Value="NaN"/>

Binding is unnecessary.

2

I came up upon the same issue in my application. In the end I changed the code so that I toggled the visibility of the content between Collapsed and Visible, and replaced the Grid with a StackPanel.

I've generally found the quality of the MS control template samples to be pretty good, but the error with this one was a bit frustrating.

2

Same problem, but the accepted solution by Carlo does not work perfectly. The problem which the poster was facing goes away but the Expander partially breaks-

if you have some content which needs to expand in an already expanded expander, it will not do so with the Binding to DesiredSize.Height, you need DesiredHeight - might be due to a reason given by user275587.

1

DesiredHeight comes from content element and it is a valid binding. I think the reason your binding does not resolve is because DesiredHeight relies on Height property and you haven't set a fixed Height in your template so it evaluates to Double.Nan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.