Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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>

Try it.

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

Binding is unnecessary.

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.

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.

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"/>

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top