views:

491

answers:

4

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?

+3  A: 

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.

Carlo
Which Framework / Border are you using? When I enter the code you supplied in my code-behind, I get: 'System.Windows.Controls.Border' does not contain a definition for 'DesiredHeight' ...
Joseph Sturtevant
Binding to 'Height' instead of 'DesiredHeight', however, does seem to resolve the error.
Joseph Sturtevant
Can you show all the XAML mark up?
Carlo
Actually, my bad, it's DesiredSize.Width, try binding to that.
Carlo
See edit on answer
Carlo
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?
Joseph Sturtevant
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
+1  A: 

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.

Drew Noakes
+1  A: 

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

+1  A: 

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>
SonicBison